SoFunction
Updated on 2025-04-06

Axios sends a post request springMVC fails to receive parameters. Solution

When axios sends a post request, the parameters cannot be received in the background. After analyzing the request, it was found that the content-type of the request header is wrong, it is application/json. Normally, it should be application/x-www-form-urlencoded.

There are three solutions:

1. Set the default request header for axios

//Set global['Content-Type'] = 'application/x-www-form-urlencoded';
var instance = ({}) // This creates only:['Content-Type'] = 'application/x-www-form-urlencoded';

2. Use URLSearchParams to build parameters

var params = new URLSearchParams();
("username", _this.username);
("password", _this.password);
("/service/login", paramsOfJson
   ).then(function (response) {
    (response);
   }).catch(function (error) {
    (error);
   })

3. Use @requestBody to receive in the background

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

The above article "Axios sends a post request" and springMVC cannot receive parameters is all the content I share with you. I hope you can give you a reference and I hope you can support me more.