When using the post of vue+axios to send a request, the data of the response cannot be obtained in the callback function of the response. The code is as follows
var vm = new Vue({
el: '#app',
data: {
obj: ''
},
methods: {
postData: function() {
var url = 'http://xxxxxxxxxx'
axios.post(url, {
id: '0',
name: 'idNmae'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
})
}
}
});
Open the NetWork option in chrom and see a warning in the request header information. Provisional headers are shown
At
this time, I thought that the interface given by the backend was in addition to the problem. In order to verify it, I used the post method in vue-resource to request this interface and found that I could get the response. At this time, the problem is definitely not an interface problem. The problem is still in the post request of axios. According to Provisional headers are shown
this warning, it should be the problem of the request header. Recall that the request header must be set when using the Ajax post request before. If yes, then use post in axios and also set the request header to see if it is possible. The changed code is as follows
var vm = new Vue({
el: '#app',
data: {
obj: ''
},
methods: {
postData: function() {
var url = 'http://xxxxxxxxxx'
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //此处是增加的代码,设置请求头的类型
axios.post(url, {
id: '0',
name: 'idNmae'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
})
}
}
});
After setting axios.defaults.headers['Content-Type']
, you can request the response information. You're done!