我有以下内容,但它不起作用,我在stackoverflow上的某个地方读到了它的工作原理,但我似乎无法使其工作..它出错了……我在做错什么吗?
如果我确实传递了这样的数据-它可以工作-所以我知道我的服务正在工作
//THIS WORKS data: "{one : 'test',two: 'test2' }" // BUT SETTING UP OBJECT doesn't work.. var saveData = {}; saveData.one = "test"; saveData.two = "tes2"; $.ajax({ type: "POST", url: "MyService.aspx/GetDate", data: saveData, contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); }, error: function(msg) { alert('error'); } });
我相信代码将在对象上调用.value或.toString(),然后通过网络传递。您想传递JSON。
因此,包括JSON JavaScript库
http://www.json.org/js.html
然后通过…
var saveData = {}; saveData.one = "test"; saveData.two = "tes2"; $.ajax({ type: "POST", url: "MyService.aspx/GetDate", data: JSON.stringify(saveData), // NOTE CHANGE HERE contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); }, error: function(msg) { alert('error'); } });