axios post request to send form data

后端 未结 9 2409
走了就别回头了
走了就别回头了 2020-11-22 03:40

axios POST request is hitting the url on the controller but setting null values to my POJO class, when I go through developer tools in chrome, the payload conta

9条回答
  •  独厮守ぢ
    2020-11-22 04:13

    You can post axios data by using [FormData()][1] like :

    var bodyFormData = new FormData();
    

    And then add the fields to the form you want to send :

    bodyFormData.append('userName', 'Fred');
    

    If you are uploading images, you may want to use .append

    bodyFormData.append('image', imageFile); 
    

    And then you can use axios post method (You can amend it accordingly)

    axios({
        method: 'post',
        url: 'myurl',
        data: bodyFormData,
        headers: {'Content-Type': 'multipart/form-data' }
        })
        .then(function (response) {
            //handle success
            console.log(response);
        })
        .catch(function (response) {
            //handle error
            console.log(response);
        });
    

    You can read more here -

    https://developer.mozilla.org/en-US/docs/Web/API/FormData https://github.com/axios/axios/issues/318

提交回复
热议问题