Home > Web Front-end > JS Tutorial > body text

Detailed instructions on using axios to solve http request problems in vue2 (detailed tutorial)

亚连
Release: 2018-06-01 11:42:20
Original
2240 people have browsed it

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
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!