Home  >  Article  >  Web Front-end  >  Solutions to problems encountered during vue resource post requests

Solutions to problems encountered during vue resource post requests

小云云
小云云Original
2018-01-05 14:55:172477browse

This article mainly introduces the pitfalls encountered when requesting vue resource post. Friends in need can refer to it. I hope it can help everyone.

Use post request

// 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);

However, this does not mean that you will not encounter problems during use: (For example, when you encounter such an error: XMLHttpRequest cannot load XXX. Response for preflight has invalid HTTP status code 405); This $http request is still a little different from jquery's ajax. The post data here is not in the form of form data by default, but the request payload. The solution is very simple: add the headers field to the vue instance:

http: {
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}

or use the simpler method provided by vue:

Vue.http.options.emulateJSON = true;

Related recommendations:

php curl get post request usage example sharing

How to solve the problem of vue integrated axios sending post request payload causing the background to be unable to receive data

How to implement Ajax GET POST request using native JS

The above is the detailed content of Solutions to problems encountered during vue resource post requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn