读取ASMX返回的JSON数据

发布于 2024-10-21 05:04:39 字数 1889 浏览 3 评论 0原文

我写了一个 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 访问时,我得到以下响应: as see in google chrome in this case

此警报的代码是:

$(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:
as seen in google chrome in this case

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-10-28 05:04:39

@rsp 的答案在技术上是正确的,但真正的问题是您在 asmx 页面中对值进行了双重编码。

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] //This will cause the response to be in JSON
    public Dictionary<string, string> IsEmailValid(string email)
    {
        Dictionary<string, string> response = new Dictionary<string, string>();
        response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
        return response; //Trust ASP.NET to do the formatting here
    }

那么你就不需要在 JavaScript 中进行双重解码。

@rsp's answer is technically correct, but the real problem is that you're doubly encoding your values in your asmx page.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] //This will cause the response to be in JSON
    public Dictionary<string, string> IsEmailValid(string email)
    {
        Dictionary<string, string> response = new Dictionary<string, string>();
        response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
        return response; //Trust ASP.NET to do the formatting here
    }

Then you don't need to double decode in the JavaScript.

恋竹姑娘 2024-10-28 05:04:39

您的反序列化 JSON 对象似乎有另一个 JSON 作为其值之一。尝试添加:

var data = $.parseJSON(msg.d);
alert(data.Response);

到您的成功回调中,看看是否是这种情况。

更新:如果是这种情况,那么您已经对数据进行了两次 JSON 编码 - 请参阅C. Ross 的答案以获得正确的解决方案。

Your deserialized JSON object seems to have another JSON as one of its values. Try adding:

var data = $.parseJSON(msg.d);
alert(data.Response);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文