Home >Web Front-end >JS Tutorial >Detailed instructions on using axios to solve http request problems in vue2 (detailed tutorial)
Below I will share with you an article to solve the problem of using axios http request in vue2. It has a good reference value and I hope it will be helpful to 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 The header is 'content-type': 'application/x-www-form-urlencoded'
So the solution is:
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. After consulting the information, I found that a qs module needed to be introduced
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);
Done!
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Angular4 integrated ng2-file-upload upload component
iview table height dynamic setting method
How to install style/css loader in vue2.0
The above is the detailed content of Detailed instructions on using axios to solve http request problems in vue2 (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!