Home >Web Front-end >JS Tutorial >Why springMVC cannot receive parameters when sending post request

Why springMVC cannot receive parameters when sending post request

php中世界最好的语言
php中世界最好的语言Original
2018-03-28 13:46:554123browse

This time I will bring you why sending post requestspringMVC cannot receive parameters, and sending post request springMVC cannot receive parameters.What are the precautions? The following is a practical case. Let’s take a look.

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';

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);
   })

3. Use @requestBody to receive in the background

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

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:

How to use the CORS of the Koa2 framework to complete cross-domain ajax requests

jQuery+JSONP cross-domain requests How to achieve

The above is the detailed content of Why springMVC cannot receive parameters when sending post request. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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