[英]Ajax POST data in Request Param
I am making a POST request as below : 我正在发出一个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");
}
});
On the server, I am using @RequestParams to get this data. 在服务器上,我使用@RequestParams来获取此数据。
@RequestParam String clientEmail, @RequestParam String clientName, @RequestParam String orgName, @RequestParam String logoURL, @RequestParam String redirectURI
I am getting below from from server: 我从服务器下面得到:
{"code":"400","errorMessage":"Required String parameter 'clientEmail' is not present"}
If I use @RequestBody instead of @RequestParam to accept this data its working fine. 如果我使用@RequestBody而不是@RequestParam接受这些数据,它的工作正常。
My question is How can I get this data in Request Parameters? 我的问题是如何在请求参数中获取此数据? What am I doing wrong?
我究竟做错了什么? I have tried Jquery($.get(), $.post()) also.
我也尝试过Jquery($。get(),$ .post())。 nothing working.
一切正常。
Thanks for any help. 谢谢你的帮助。
I just did small project with latest version of spring boot and jquery and it is working well, and based on investigation I did I found there are 2 factor can make this issue, one from jquery and other one from Spring MVC converters: 我刚刚用最新版本的spring boot和jquery做了一个小项目,它运行良好,根据调查我发现有2个因素可以解决这个问题,一个来自jquery,另一个来自Spring MVC转换器:
1- jquery ajax have contentType parameter 1- jquery ajax有contentType参数
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
if this one changed to application/json
or application/xml
will change the way it is sending request to server then will make issue to server parsing, but it is default value will send form as key=value
coma separated and this is OK with FormHttpMessageConverter
"which is drive us to next point" 如果这个更改为
application/json
或者application/xml
将改变它向服务器发送请求的方式然后会对服务器解析产生问题,但它是默认值将发送表单作为key=value
coma分离,这对于FormHttpMessageConverter
是可以的“这将我们带到下一点”
2- spring MVC is using FormHttpMessageConverter
for "application/x-www-form-urlencoded"
parsing or converting then you can use @RequestParam
if this converter changed to other converter like: 2- spring MVC正在使用
FormHttpMessageConverter
进行"application/x-www-form-urlencoded"
解析或转换,如果此转换器更改为其他转换器,则可以使用@RequestParam
:
MappingJackson2HttpMessageConverter
for 'application/json'
MappingJackson2HttpMessageConverter
为'application/json'
or 要么
Jaxb2CollectionHttpMessageConverter
for 'application/xml'
'application/xml'
Jaxb2CollectionHttpMessageConverter
so it will expect another request and you can get it using @RequestBody
所以它会期待另一个请求,你可以使用
@RequestBody
获得它
So, you have to check request going from jquery is it form, json, or xml using development tools in your browser, then check you spring code/configuration to be sure that this request is converted by the FormHttpMessageConverter
, this converter could be changed by the parameters of your @RequestMapping
. 因此,您必须使用浏览器中的开发工具检查来自jquery的请求是form,json还是xml,然后检查spring命令/配置以确保此请求由
FormHttpMessageConverter
转换,此转换器可以通过以下方式更改: @RequestMapping
的参数。
you cant use $.get with payload(data) but you can use $.post. 你不能使用$ .get与有效载荷(数据),但你可以使用$ .post。 please add attribute contentType in your request parameter.
请在您的请求参数中添加属性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.