当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue resource post请求时遇到的坑

vue resource post请求时遇到的坑

2017年12月12日  | 移动技术网IT编程  | 我要评论

顶针,星际猎手txt,北宋贼行天下

使用 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;

总结

以上所述是小编给大家介绍的vue resource post请求时遇到的坑,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网