Home > Web Front-end > JS Tutorial > Sending a post request through axios found that springMVC could not receive parameters (detailed tutorial)

Sending a post request through axios found that springMVC could not receive parameters (detailed tutorial)

亚连
Release: 2018-06-01 11:29:13
Original
2883 people have browsed it

Below I will share with you a solution to the problem that springMVC cannot receive parameters when sending a post request in axios. It has a good reference value and I hope it will be helpful to everyone.

When axios sent a post request, there was a situation where the parameters could not be received in the background. After analyzing the request, I found that the content-type of the request header was wrong. It was application/json. Normally it should be application/x-www- form-urlencoded.

There are three solutions:

1. Set the default request header of axios

//设置全局的
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
var instance = axios.create({}) // 这样创建出来的 只需要:
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Copy after login

2. Use URLSearchParams to construct parameters

var params = new URLSearchParams();
params.append("username", _this.username);
params.append("password", _this.password);
axios.post("/service/login", paramsOfJson
   ).then(function (response) {
    console.log(response);
   }).catch(function (error) {
    console.log(error);
   })
Copy after login

3. Use @requestBody to receive in the background

@PostMapping(value = "/login")
public String testLogin(@RequestBody Map dataMap)
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

vue filter filter instance detailed explanation

The vue page switches to the scroll page to display the top instance_vue.js

Let’s talk about the use of JS animation library Velocity.js

The above is the detailed content of Sending a post request through axios found that springMVC could not receive parameters (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