admin管理员组文章数量:1279089
i am trying to post data though ajax post to spring controller. My ajax code is
function postData(tag){
console.debug(tag);
var targetUrl = "/add/tag";
$.ajax({
url : targetUrl,
type : "POST",
data : tag,
dataType : "text",
success : function(response){
console.debug(response);
},
error : function(){
console.debug("error : ".concat(response));
}
});
}
and my controller code is
@RequestMapping(value = "/add/tag", method = POST, consumes = { "application/json" },headers = "content-type=application/x-www-form-urlencoded")
@ResponseBody
public Integer addTag(HttpServletRequest request,
@PathVariable("uid") String gatheringUid, @RequestBody String tag) {
System.out.print(tag);
return gatheringService.updateGathering(gatheringUid, tags);
}
on server side it prints the value of tag appended by "=" sign, while on firebug console value prints as i enterd.
For example when i post data "test", on firebug console it prints "test" and on server side console it prints "test=".
Can any one kindly tell me what is the problem here.
Thanks in advance, regards.
i am trying to post data though ajax post to spring controller. My ajax code is
function postData(tag){
console.debug(tag);
var targetUrl = "/add/tag";
$.ajax({
url : targetUrl,
type : "POST",
data : tag,
dataType : "text",
success : function(response){
console.debug(response);
},
error : function(){
console.debug("error : ".concat(response));
}
});
}
and my controller code is
@RequestMapping(value = "/add/tag", method = POST, consumes = { "application/json" },headers = "content-type=application/x-www-form-urlencoded")
@ResponseBody
public Integer addTag(HttpServletRequest request,
@PathVariable("uid") String gatheringUid, @RequestBody String tag) {
System.out.print(tag);
return gatheringService.updateGathering(gatheringUid, tags);
}
on server side it prints the value of tag appended by "=" sign, while on firebug console value prints as i enterd.
For example when i post data "test", on firebug console it prints "test" and on server side console it prints "test=".
Can any one kindly tell me what is the problem here.
Thanks in advance, regards.
Share Improve this question edited Feb 26, 2014 at 9:19 Shahzeb Khan asked Feb 26, 2014 at 6:08 Shahzeb KhanShahzeb Khan 3,6428 gold badges46 silver badges82 bronze badges 3- 1 you're sending dataType 'text' and consuming application/json. You probably want to change your java to consumes text/plain or the equivalent. – Geoff Genz Commented Feb 26, 2014 at 6:10
- 1 Geoff has a point. You are printing the request and not the tag. – Haim Raman Commented Feb 26, 2014 at 6:39
- Your handler method is pletely contradictory. – Sotirios Delimanolis Commented Feb 26, 2014 at 6:43
2 Answers
Reset to default 11This is a consequence of AJAX sending your POST with a content-type of application/x-www-form-urlencoded
.
Spring uses a StringHttpMessageConverter
to resolve the argument to bind to a @RequestBody
annotated String
parameter. Internally, this checks if the request was a form POST. If it is, it deserializes the whole body as if it was a form submission. In this case, a single word text
, appears as if it was, for example, a single <input>
element with no value, ie. text=
.
If you're curious, this is done in ServletServerHttpRequest#getBodyFromServletRequestParameters(..)
.
Change your content-type to something more appropriate, maybe text/plain
. Don't use dataType
. Use contentType
or the headers
.
FYI, based on Sotirios' answer, the following worked in the Ajax jQuery code.
$.ajax({
type : "post",
dataType : 'json',
contentType : 'text/plain', // This was added to delete the =
url : 'myURL',
data : id
})
本文标签: javascriptajax post to spring mvc appends quotquot sign to request dataStack Overflow
版权声明:本文标题:javascript - ajax post to spring mvc appends "=" sign to request data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292050a2370607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论