我认为数据需要是字符串。对象将被转换为查询字符串,这就是你在这里看到的。
你可以使用JSON.stringify(obj)
方法将你的对象转换为字符串。JSON对象的代码可从以下链接获取:https://github.com/douglascrockford/JSON-js/blob/master/json2.js。
或者,只需将你正在使用创建对象的代码直接作为文字字符串传递,但我想象这只是一个例子,你可能想要编码某个已经创建好的对象。
如果你的应用程序总是需要发送JSON,则可以在初始化某个位置执行以下代码,然后像你的示例中一样使用默认的$.ajax
调用,它将始终序列化为JSON字符串而不是Ajax默认查询字符串。
这里我使用上面提到的JSON对象:
$.ajaxSetup({
contentType : 'application/json',
processData : false
});
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
if (options.data){
options.data=JSON.stringify(options.data);
}
});
//url: this is a reference to the XML, where you need to define the mapping.
//<entry key="/getEmpDetails/transEfileGenerate.app">
//<bean class="com.adp.ems.framework.spring.MappingItem" p:delegate-ref="efilePageDelegate"
//p:action="passJSONObjectAndGetWebServiceOutput" />
//str1 is the input JSON that you need to pass... Ajax will automatically take care to get the response.
//</entry>
var kw = {
url : "getEmpDetails/transEfileGenerate.app",
timeout : 30000,
handleAs : "json",
sync: false,
putData : str1,
headers: { "Content-Type": "application/json"},
load : function(result) {
},