【发布时间】:2014-03-28 19:07:06
【问题描述】:
我正在尝试通过 ajax post 将数据发布到 spring 控制器。我的 ajax 代码是
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));
}
});
}
我的控制器代码是
@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);
}
在服务器端,它打印带有“=”符号的标记值,而在萤火虫控制台上,当我输入时打印值。
例如,当我发布数据“test”时,在 firebug 控制台上打印“test”,在服务器端控制台上打印“test=”。
谁能告诉我这里有什么问题。
提前致谢, 问候。
【问题讨论】:
-
您正在发送 dataType 'text' 并使用 application/json。您可能希望将您的 java 更改为使用 text/plain 或等价物。
-
Geoff 说得有道理。您正在打印请求而不是标签。
-
你的handler方法完全矛盾。
标签: javascript jquery ajax spring-mvc