Vue Resource Post Request Content-Type

Ramazan APAYDIN picture Ramazan APAYDIN · Sep 11, 2016 · Viewed 10.1k times · Source

Vue-Resource Post Request:

this.$http.post(form.action, new FormData(form)).then(function (response) {
    FetchResponse.fetch(this, response.data)
})

Request are Send as Content-Type:"application/json;charset=utf-8" But No data can be displayed by PHP Post.

Set Up Header Vue-Resource:

request.headers.set('Content-Type', '');

But Request Content-Type:", multipart/form-data; boundary=----WebKitFormBoundaryTsrUACAFB1wuhFOR"

there is a comma at the beginning of the query.

Jquery Post Request:

$.ajax({
    url     : form.action,
    type    : 'POST',
    data    : new FormData(form),
    success : function (reqData) {
        FetchResponse.fetch(ss, reqData)
    },
});

The same query works seamlessly with jQuery. jQuery Content-Type: "multipart/form-data; boundary=----WebKitFormBoundaryTsrUACAFB1wuhFOR"

Issue: https://github.com/vuejs/vue-resource/issues/398

Answer

Zacky Pickholz picture Zacky Pickholz · Mar 7, 2017

Please try instead to post a simple JSON object and enable the 'emulateJSON' vue-resource option:

const formData = {
    someProp: this.someProp,
    someValue: 'some value'
};

this.$http.post(this.postUrl, formData, {emulateJSON: true})
   .then(response => {
        console.log(response.body);
    }, response => {
       console.error(response.body);
    });