我正在尝试使用Dojo post(即Dojo.xhrPost()方法发布Json数据。我发现了许多没有解决我问题的解决方案。我正在传递内容类型为'application/json'的json数据。下面是客户端(JS)和服务器(spring MVC控制器)的代码片段。
我在我的Lib文件夹中添加了以下jar用于JSON转换
1.Jackson-Core-ASL-1.9.10.JAR
2.Jackson-Mapper-ASL-1.9.10.jar
3.Jackson-jaxrs-1.9.13.jar
要点:传递的JSON数据格式正确。JSP是我的视图解析器。
var map = dojo.toJson({id:'1',name:'er'})
var xhrArgs = {
url : _context+'/exchangeRate/save-exchange',
postData : map,
handleAs: "json",
headers : {
'Content-Type' : 'application/json,charset=utf-8',
},
load : function(response) {
console.log(response);
},
error : function(error_msg, details) {
alert(error_msg);
console.log(details)
}
}
dojo.xhrPost(xhrArgs);
下面是我的spring控制器方法
@RequestMapping(值=“/ExchangeRate/Save-Exchange”,方法=
RequestMethod.Post,headers={“content-type=application/json”},consumes
=“application/json”,products=“application/json”)
public@responsebody
Map saveExchangeRate(@RequestBody Map Map){
返回新的Hashmap();
}
添加ACCEPT-header到accept application/json怎么样?
headers : {
'Accept' : 'application/json',
},
此外,您的内容类型头的格式不正确。使用;
代替,
。
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json;charset=utf-8',
},
使用
contentType:'application/json',
代替
handleas:“json”,