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

Why can't axios http request be used in vue2?

php中世界最好的语言
Release: 2018-03-28 14:06:14
Original
2495 people have browsed it

This time I will bring you why axios http request cannot be used in vue2, and what are the precautions to solve the problem that axios http cannot be used in vue2. The following is a practical case, let's take a look.

When using axios to handle post requests , the problem that occurs is solved

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 Instantiate a new axios and set its message header to '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!

problem solved!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of deep and shallow copy of JS

Display of http request and loading in Vue2.0

The above is the detailed content of Why can't axios http request be used in vue2?. 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!