Home >Web Front-end >JS Tutorial >Problems and solutions when using axios http request in vue2
This article mainly shares with you an article to solve the problems that arise when using axios http request in vue2. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Solution to the problems that arise when using axios to process post requests
By default: axios.post(url, params).then(res => res.data);
When the url is a remote interface link, a 404 error will be reported:
Uncaught (in promise) Error: Request failed with status code 404
We need to instantiate a new axios and set its message header to 'content-type': ' application/x-www-form-urlencoded'
So I came up with the solution:
var instance = axios.create({ headers: {'content-type': 'application/x-www-form-urlencoded'} }); instance .post(`url`, params).then(res => res.data);
Then I found that no error was reported, but the background could not accept the incoming parameters. I checked the information and found that it needed to be introduced. A qs module
var qs=require('qs'); var instance = axios.create({ headers: {'content-type': 'application/x-www-form-urlencoded'} }); instance .post(`url`, qs.stringify(params)).then(res => res.data);
is done!
problem solved!
Related recommendations:
How node.js implements network requests through axios
## Detailed explanation of VueJs building Axios interface request tool examples
About vue2.0 setting proxyTable to use axios for cross-domain requests
The above is the detailed content of Problems and solutions when using axios http request in vue2. For more information, please follow other related articles on the PHP Chinese website!