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

Problems and solutions when using axios http request in vue2

小云云
Release: 2018-03-06 09:51:52
Original
3465 people have browsed it

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

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

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

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!

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