本文主要介绍了vue resource post请求时遇到的坑,需要的朋友可以参考下,希望能帮助到大家。
使用 post 请求
// global Vue object
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
// in a Vue instance
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);然而,这并不代表使用过程中不会遇到问题:(比如使用时遇到这样的报错:XMLHttpRequest cannot load XXX. Response for preflight has invalid HTTP status code 405);这个$http请求和jquery的ajax还是有点区别,这里的post的data默认不是以form data的形式,而是request payload。解决起来倒也很简单:在vue实例中添加headers字段:
http: {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}或者使用 vue 方面提供的更加简单做法:
Vue.http.options.emulateJSON = true;
相关推荐:
立即学习“前端免费学习笔记(深入)”;










