Ajax POST 请求参数中的数据
Ajax POST data in Request Param
我正在提出一个 POST 请求,如下所示:
$.ajax({
url :"/clientCredentials.json",
type: "POST",
data: {
"clientEmail": email,
"clientName":clientName,
"orgName":orgName,
"logoURL":logoURL,
"redirectURI":redirectUri
},
success: function(response){
alert("sucess");
},
error:function(response){
alert("something went wrong");
}
});
在服务器上,我使用@RequestParams 来获取此数据。
@RequestParam String clientEmail, @RequestParam String clientName, @RequestParam String orgName, @RequestParam String logoURL, @RequestParam String redirectURI
我正在从服务器获取以下内容:
{"code":"400","errorMessage":"Required String parameter 'clientEmail' is not present"}
如果我使用@RequestBody 而不是@RequestParam 来接受这个数据,它工作正常。
我的问题是如何在请求参数中获取这些数据?我究竟做错了什么?
我也试过 Jquery($.get(), $.post()) 。没有任何效果。
感谢您的帮助。
你不能将 $.get 与 payload(data) 一起使用,但你可以使用 $.post。请在您的请求参数中添加属性 contentType。
$.ajax({
url :"/clientCredentials.json",
type: "POST",
contentType: "application/json",
data: {
"clientEmail": email,
"clientName":clientName,
"orgName":orgName,
"logoURL":logoURL,
"redirectURI":redirectUri
},
success: function(response){
alert("sucess");
},
error:function(response){
alert("something went wrong");
}
});
我刚刚用最新版本的 spring 引导和 jquery 做了一个小项目,它运行良好,根据我所做的调查,我发现有 2 个因素可以导致这个问题,一个来自 jquery,另一个来自 Spring MVC 转换器:
1- jquery ajax 有 contentType 参数
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
如果将此更改为 application/json
或 application/xml
将更改它向服务器发送请求的方式然后将对服务器解析产生问题,但默认值将发送表单为 key=value
逗号分开,这可以用 FormHttpMessageConverter
"which is drive us to next point"
2- spring MVC 正在使用 FormHttpMessageConverter
进行 "application/x-www-form-urlencoded"
解析或转换,那么如果此转换器更改为其他转换器,您可以使用 @RequestParam
,例如:
MappingJackson2HttpMessageConverter
对于 'application/json'
或
Jaxb2CollectionHttpMessageConverter
对于 'application/xml'
所以它会期待另一个请求,您可以使用 @RequestBody
获取它
因此,您必须使用浏览器中的开发工具检查来自 jquery 的请求是 json 还是 xml,然后检查您 spring code/configuration 确保此请求由 FormHttpMessageConverter
转换,此转换器可由您的 @RequestMapping
.
的参数更改
我正在提出一个 POST 请求,如下所示:
$.ajax({
url :"/clientCredentials.json",
type: "POST",
data: {
"clientEmail": email,
"clientName":clientName,
"orgName":orgName,
"logoURL":logoURL,
"redirectURI":redirectUri
},
success: function(response){
alert("sucess");
},
error:function(response){
alert("something went wrong");
}
});
在服务器上,我使用@RequestParams 来获取此数据。
@RequestParam String clientEmail, @RequestParam String clientName, @RequestParam String orgName, @RequestParam String logoURL, @RequestParam String redirectURI
我正在从服务器获取以下内容:
{"code":"400","errorMessage":"Required String parameter 'clientEmail' is not present"}
如果我使用@RequestBody 而不是@RequestParam 来接受这个数据,它工作正常。
我的问题是如何在请求参数中获取这些数据?我究竟做错了什么? 我也试过 Jquery($.get(), $.post()) 。没有任何效果。
感谢您的帮助。
你不能将 $.get 与 payload(data) 一起使用,但你可以使用 $.post。请在您的请求参数中添加属性 contentType。
$.ajax({
url :"/clientCredentials.json",
type: "POST",
contentType: "application/json",
data: {
"clientEmail": email,
"clientName":clientName,
"orgName":orgName,
"logoURL":logoURL,
"redirectURI":redirectUri
},
success: function(response){
alert("sucess");
},
error:function(response){
alert("something went wrong");
}
});
我刚刚用最新版本的 spring 引导和 jquery 做了一个小项目,它运行良好,根据我所做的调查,我发现有 2 个因素可以导致这个问题,一个来自 jquery,另一个来自 Spring MVC 转换器:
1- jquery ajax 有 contentType 参数
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
如果将此更改为 application/json
或 application/xml
将更改它向服务器发送请求的方式然后将对服务器解析产生问题,但默认值将发送表单为 key=value
逗号分开,这可以用 FormHttpMessageConverter
"which is drive us to next point"
2- spring MVC 正在使用 FormHttpMessageConverter
进行 "application/x-www-form-urlencoded"
解析或转换,那么如果此转换器更改为其他转换器,您可以使用 @RequestParam
,例如:
MappingJackson2HttpMessageConverter
对于 'application/json'
或
Jaxb2CollectionHttpMessageConverter
对于 'application/xml'
所以它会期待另一个请求,您可以使用 @RequestBody
因此,您必须使用浏览器中的开发工具检查来自 jquery 的请求是 json 还是 xml,然后检查您 spring code/configuration 确保此请求由 FormHttpMessageConverter
转换,此转换器可由您的 @RequestMapping
.