欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue resource post请求时遇到的坑

程序员文章站 2022-09-07 23:27:14
使用 post 请求 // global vue object vue.http.get('/someurl', [options]).then(success...

使用 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请求时遇到的坑,希望对大家有所帮助