I have the following but it's not working, I read somewhere on the * that it works like this but I can't seem to get it to work.. it errors... am I doing something wrong?
我有以下但它不工作,我在*上的某处读到它的工作方式,但我似乎无法让它工作..它的错误......我做错了什么?
If I do pass data like this - it works -- so I know my service is working
如果我确实传递了这样的数据 - 它有效 - 所以我知道我的服务正在运行
//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');
}
});
3 个解决方案
#1
23
I believe that code is going to call .value or .toString() on your object and then pass over the wire. You want to pass JSON.
我相信代码会在你的对象上调用.value或.toString()然后传递电线。你想传递JSON。
So, include the json javascript library
所以,包括json javascript库
http://www.json.org/js.html
And then pass...
然后通过......
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');
}
});
#2
4
According to this blog post, the reason it doesn't work when you try to pass the object is that jQuery attempts to serialize it. From the post:
根据这篇博客文章,当你尝试传递对象时它不起作用的原因是jQuery尝试序列化它。从帖子:
Instead of passing that JSON object through to the web service, jQuery will automatically serialize and send it as:
jQuery不会将该JSON对象传递给Web服务,而是自动序列化并将其发送为:
fname=dave&lname=ward
To which, the server will respond with:
对此,服务器将响应:
Invalid JSON primitive: fname.
This is clearly not what we want to happen. The solution is to make sure that you’re passing jQuery a string for the data parameter[...]
这显然不是我们想要发生的事情。解决方案是确保您为数据参数传递jQuery字符串[...]
Which is what you're doing in the example that works.
这是你在有效的例子中做的。
#3
3
My suggestion would be to use the jquery-json plug-in and then you can just do this in your code:
我的建议是使用jquery-json插件,然后你可以在你的代码中执行此操作:
...
data: $.toJSON(saveData),
...
#1
23
I believe that code is going to call .value or .toString() on your object and then pass over the wire. You want to pass JSON.
我相信代码会在你的对象上调用.value或.toString()然后传递电线。你想传递JSON。
So, include the json javascript library
所以,包括json javascript库
http://www.json.org/js.html
And then pass...
然后通过......
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');
}
});
#2
4
According to this blog post, the reason it doesn't work when you try to pass the object is that jQuery attempts to serialize it. From the post:
根据这篇博客文章,当你尝试传递对象时它不起作用的原因是jQuery尝试序列化它。从帖子:
Instead of passing that JSON object through to the web service, jQuery will automatically serialize and send it as:
jQuery不会将该JSON对象传递给Web服务,而是自动序列化并将其发送为:
fname=dave&lname=ward
To which, the server will respond with:
对此,服务器将响应:
Invalid JSON primitive: fname.
This is clearly not what we want to happen. The solution is to make sure that you’re passing jQuery a string for the data parameter[...]
这显然不是我们想要发生的事情。解决方案是确保您为数据参数传递jQuery字符串[...]
Which is what you're doing in the example that works.
这是你在有效的例子中做的。
#3
3
My suggestion would be to use the jquery-json plug-in and then you can just do this in your code:
我的建议是使用jquery-json插件,然后你可以在你的代码中执行此操作:
...
data: $.toJSON(saveData),
...