![](/img/trans.png)
[英]jQuery or AngularJS Ajax “POST” request to server using code below however do not sending param data to server.
[英]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())。 一切正常。
谢谢你的帮助。
我刚刚用最新版本的spring boot和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
coma分离,这对于FormHttpMessageConverter
是可以的“这将我们带到下一点”
2- spring MVC正在使用FormHttpMessageConverter
进行"application/x-www-form-urlencoded"
解析或转换,如果此转换器更改为其他转换器,则可以使用@RequestParam
:
MappingJackson2HttpMessageConverter
为'application/json'
要么
'application/xml'
Jaxb2CollectionHttpMessageConverter
所以它会期待另一个请求,你可以使用@RequestBody
获得它
因此,您必须使用浏览器中的开发工具检查来自jquery的请求是form,json还是xml,然后检查spring命令/配置以确保此请求由FormHttpMessageConverter
转换,此转换器可以通过以下方式更改: @RequestMapping
的参数。
你不能使用$ .get与有效载荷(数据),但你可以使用$ .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");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.