I have the following jQuery code:
我有以下jQuery代码:
$.ajax({
type: "POST",
url: "Services/MyService.asmx/Select",
dataType: "json",
data: "{'data':'test'}",
contentType: "application/json; charset=utf-8",
success: function(msg){
alert(msg);
},
error: function(xhr){ alert(xhr.statusText);}
});
The call to the method returns the following:
对该方法的调用返回以下内容:
"{"FirstName":"James"}"
When I get the value back, my alert returns the full json string. If I try to do alert(msg.FirstName)
, I get "undefined".
当我返回值时,我的警报返回完整的json字符串。如果我尝试做警报(msg.FirstName),我会得到“未定义”。
I have seen a lot of examples using the getJSON() method; however, I haven't seen a way to use that for a POST verb. Can anyone point me in the right direction where I'm going wrong? Based on the jquery documentation, the return value should be the same dataType (json) so I'm not certain what I'm missing.
我已经看到很多使用getJSON()方法的例子;但是,我还没有看到将其用于POST动词的方法。任何人都可以指出我在哪里出错了吗?根据jquery文档,返回值应该是相同的dataType(json),所以我不确定我缺少什么。
EDIT: I looked on my service and it is matching examples that I'm finding in terms of the method signature returning a string. I've also confirmed the response type is of application/json.
编辑:我看了我的服务,它是匹配的示例,我发现方法签名返回一个字符串。我还确认响应类型是application / json。
EDIT2: Updated the response to include the outside quotes. I'm also using a custom JavaScriptConverter to do the JSON serialization. The custom converter just takes my object properties (in this case FirstName) and loads it and it's value into a Dictionary collection which the ASP.Net AJAX Extensions v1.0 can serialize easily.
EDIT2:更新了响应以包含外部引号。我也使用自定义JavaScriptConverter来执行JSON序列化。自定义转换器只获取我的对象属性(在本例中为FirstName)并将其加载到Dictionary集合中,ASP.Net AJAX Extensions v1.0可以轻松地序列化。
EDIT3: Looking into the issue that I was having with eval() (it caused an Expected ";"
error), I noticed that the json property names were also enclosed in quotes. Once I removed the quotes from the property name (not the value), eval() worked again. Looking into the server side of this issue now.
编辑3:调查我使用eval()的问题(它导致了一个预期的“;”错误),我注意到json属性名称也用引号括起来。一旦我从属性名称(而不是值)中删除了引号,eval()再次工作。现在调查此问题的服务器端。
5 个解决方案
#1
5
The jQuery .ajax
usage looks solid. How are you verifying the data returned? Firebug? Fiddler? Because .asmx
webservices don't return data like {"FirstName":"James"}
. Responses look more like:
jQuery .ajax用法看起来很稳固。您如何验证返回的数据?萤火虫?提琴手?因为.asmx webservices不返回像{“FirstName”:“James”}这样的数据。回应看起来更像:
{"d":{"FirstName":"James"}}
(See http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/)
For your success
callback, try:
为了您的成功回调,请尝试:
function(res) { alert(res.d.FirstName) }
Edit: Saw your updates and comment re v1.0 of the ASP.Net AJAX:
编辑:看到您的更新并评论ASP.Net AJAX的v1.0:
I'm not positive how v1.0 works wrt serializing your response, but my guess is if you're doing your own custom JSON serialization in your WebService method, the response may be getting JSON serialized again. So you're serializing twice.
我不是肯定v1.0如何工作来序列化您的响应,但我的猜测是,如果您在WebService方法中进行自己的自定义JSON序列化,响应可能会再次获得JSON序列化。所以你要两次序列化。
I believe all the components you're using are doing what they're supposed to, it's just now your success
callback needs to unserialize manually since you're serializing manually on the server:
我相信你正在使用的所有组件正在做他们应该做的事情,现在你的成功回调需要手动反序列化,因为你在服务器上手动序列化:
function(res) {
res = eval('(' + res + ')');
alert(res.FirstName);
}
#2
2
IIRC you can eval the string, and the result will be the json object.
IIRC你可以评估字符串,结果将是json对象。
myJSON = eval(jsonString);
myJSON = eval(jsonString);
#3
1
You can define your post variables with a JSON object as the second parameter.
您可以使用JSON对象作为第二个参数来定义post变量。
Example:
$.getJSON("service.py",
{ foo: bar },
function(data) {
$.each(data, function() { // blah });
}
);
EDIT: I see what you mean now. Does your service return "something/json" as the MIME type? If you're looking at the response headers in Firebug, it should look something like this:
编辑:我明白你的意思了。您的服务是否将“something / json”作为MIME类型返回?如果您正在查看Firebug中的响应标头,它应该如下所示:
Content-Type application/json; charset=utf-8
#4
0
try something like this :
尝试这样的事情:
result = eval('(' + result.d + ')');
#5
0
You can't use $getJSON
with ASMX services serialized through System.Web.Extensions
. You're making the call correctly with $.ajax()
.
您不能将$ getJSON与通过System.Web.Extensions序列化的ASMX服务一起使用。您正在使用$ .ajax()正确拨打电话。
Have you tried using Firebug to set a breakpoint inside the success handler and inspecting the msg value?
您是否尝试过使用Firebug在成功处理程序中设置断点并检查msg值?
#1
5
The jQuery .ajax
usage looks solid. How are you verifying the data returned? Firebug? Fiddler? Because .asmx
webservices don't return data like {"FirstName":"James"}
. Responses look more like:
jQuery .ajax用法看起来很稳固。您如何验证返回的数据?萤火虫?提琴手?因为.asmx webservices不返回像{“FirstName”:“James”}这样的数据。回应看起来更像:
{"d":{"FirstName":"James"}}
(See http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/)
For your success
callback, try:
为了您的成功回调,请尝试:
function(res) { alert(res.d.FirstName) }
Edit: Saw your updates and comment re v1.0 of the ASP.Net AJAX:
编辑:看到您的更新并评论ASP.Net AJAX的v1.0:
I'm not positive how v1.0 works wrt serializing your response, but my guess is if you're doing your own custom JSON serialization in your WebService method, the response may be getting JSON serialized again. So you're serializing twice.
我不是肯定v1.0如何工作来序列化您的响应,但我的猜测是,如果您在WebService方法中进行自己的自定义JSON序列化,响应可能会再次获得JSON序列化。所以你要两次序列化。
I believe all the components you're using are doing what they're supposed to, it's just now your success
callback needs to unserialize manually since you're serializing manually on the server:
我相信你正在使用的所有组件正在做他们应该做的事情,现在你的成功回调需要手动反序列化,因为你在服务器上手动序列化:
function(res) {
res = eval('(' + res + ')');
alert(res.FirstName);
}
#2
2
IIRC you can eval the string, and the result will be the json object.
IIRC你可以评估字符串,结果将是json对象。
myJSON = eval(jsonString);
myJSON = eval(jsonString);
#3
1
You can define your post variables with a JSON object as the second parameter.
您可以使用JSON对象作为第二个参数来定义post变量。
Example:
$.getJSON("service.py",
{ foo: bar },
function(data) {
$.each(data, function() { // blah });
}
);
EDIT: I see what you mean now. Does your service return "something/json" as the MIME type? If you're looking at the response headers in Firebug, it should look something like this:
编辑:我明白你的意思了。您的服务是否将“something / json”作为MIME类型返回?如果您正在查看Firebug中的响应标头,它应该如下所示:
Content-Type application/json; charset=utf-8
#4
0
try something like this :
尝试这样的事情:
result = eval('(' + result.d + ')');
#5
0
You can't use $getJSON
with ASMX services serialized through System.Web.Extensions
. You're making the call correctly with $.ajax()
.
您不能将$ getJSON与通过System.Web.Extensions序列化的ASMX服务一起使用。您正在使用$ .ajax()正确拨打电话。
Have you tried using Firebug to set a breakpoint inside the success handler and inspecting the msg value?
您是否尝试过使用Firebug在成功处理程序中设置断点并检查msg值?