读取ASMX返回的JSON数据
我写了一个 ASMX 服务,看起来像这样;
namespace AtomicService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Validation : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string IsEmailValid(string email)
{
Dictionary<string, string> response = new Dictionary<string, string>();
response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
}
}
我使用 Newtonsoft.Json 库来提供 JsonConvert.SerializeObject 功能。当调用 Fiddler 或通过 Jquery 访问时,我得到以下响应:
此警报的代码是:
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://127.0.0.1/AtomicService/Validation.asmx/IsEmailValid",
data: "{'email':'[email protected]'}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
if (msg["d"].length > 0) {
alert("fish");
}
alert("success: " + msg.d);
},
error: function (msg) {
alert("error");
}
});
});
虽然我可以看到 来自 msg.d
的数据,我无法访问它。我想知道响应
是什么。我怎样才能得到它?
我并不完全相信我的 ASMX 会返回正确类型的 JSON 来完成所有工作。
有人可以帮忙吗? :)
I have written an ASMX service that looks like thus;
namespace AtomicService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Validation : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string IsEmailValid(string email)
{
Dictionary<string, string> response = new Dictionary<string, string>();
response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
}
}
I'm using the Newtonsoft.Json library to provide the JsonConvert.SerializeObject functionality. When call in Fiddler or accessed via my Jquery, I get this response:
The code for this alert is:
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://127.0.0.1/AtomicService/Validation.asmx/IsEmailValid",
data: "{'email':'[email protected]'}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
if (msg["d"].length > 0) {
alert("fish");
}
alert("success: " + msg.d);
},
error: function (msg) {
alert("error");
}
});
});
And although I can see the data from the msg.d
, I can't access it. I want to know what the Response
is. How can I get at it?
I'm not entirely convinced my ASMX is returning the right type of JSON for this to all work.
Can anyone help? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@rsp 的答案在技术上是正确的,但真正的问题是您在 asmx 页面中对值进行了双重编码。
那么你就不需要在 JavaScript 中进行双重解码。
@rsp's answer is technically correct, but the real problem is that you're doubly encoding your values in your asmx page.
Then you don't need to double decode in the JavaScript.
您的反序列化 JSON 对象似乎有另一个 JSON 作为其值之一。尝试添加:
到您的成功回调中,看看是否是这种情况。
更新:如果是这种情况,那么您已经对数据进行了两次 JSON 编码 - 请参阅C. Ross 的答案以获得正确的解决方案。
Your deserialized JSON object seems to have another JSON as one of its values. Try adding:
to your success callback to see if that's the case.
UPDATE: If that's the case then you have JSON-encoded your data twice – see the answer by C. Ross for a proper solution.