【问题标题】:Getting jsonString via Jquery coming up empty通过 Jquery 获取 jsonString 为空
【发布时间】:2012-06-11 16:36:38
【问题描述】:

我正在使用 asp mvc,我正在像这样在我的程序的一部分中获取数据

 public List<IncidentPerAreaCount> getIncident()
    {
        int RondeboschCounter = 0;
        int ClaremontCounter = 0;
        int AthloneCounter = 0;
        List<IncidentPerAreaCount> IncidentAreaCount = new List<IncidentPerAreaCount>();
        IncidentPerAreaCount Rondebosch = new IncidentPerAreaCount();
        IncidentPerAreaCount Claremont = new IncidentPerAreaCount();
        IncidentPerAreaCount Athlone = new IncidentPerAreaCount();

        List<Report> Reports = GetReports();
        for (int i = 0; i < Reports.Count(); i++)
        {
            if (Reports.AsEnumerable().ElementAt(i).Area == "Rondebosch")
            {
                RondeboschCounter++;
            }
            else if (Reports.AsEnumerable().ElementAt(i).Area == "Claremont")
            {
                ClaremontCounter++;
            }
            else if (Reports.AsEnumerable().ElementAt(i).Area == "Athlone")
            {
                AthloneCounter++;
            }

        }
        Rondebosch.AreaName = "Rondebosch";
        Rondebosch.NumberOfIncidents = RondeboschCounter;
        Claremont.AreaName = "Claremont";
        Claremont.NumberOfIncidents = ClaremontCounter;
        Athlone.AreaName = "Athlone";
        Athlone.NumberOfIncidents = AthloneCounter;

        IncidentAreaCount.Add(Rondebosch);
        IncidentAreaCount.Add(Claremont);
        IncidentAreaCount.Add(Athlone);

        return IncidentAreaCount;
    }

然后我试图通过 Jquery 获取这个字符串

 var Reports = [];
    $.ajax({
    url: "Home/getIncident",
    async: false,
    dataType: 'json',
    success: function (json) { Reports = json.whatever; }
    });
    alert(Reports);

但是,警报函数总是出现空(即空文本框),而不是带有数据的 json 格式字符串。

请帮忙...

【问题讨论】:

  • 当然可以。欢迎来到 AJAX 的世界

标签: jquery asp.net-mvc json linq


【解决方案1】:

您将警报放在错误的位置。

$.ajax({
    url: "Home/getIncident",
    async: false,
    dataType: 'json',
    success: function (json) {
        Reports = json.whatever; 
        alert(Reports); // should be here.
    }
});

在开始编写代码之前,请阅读 thisthis

【讨论】:

  • 试过了,但是弹出的提示框还是空的
【解决方案2】:

您可以在 ajax 的成功函数中获取数据,而不是在 ajax 之外。尝试在成功中移动警报,然后您将获得数据。

var Reports = [];
        $.ajax({
        url: "Home/getIncident",
        async: false,
        dataType: 'json',
        success: function (json) { 
                 Reports = json.whatever; 
                 alert(Reports); //Right place
        }
        });
        alert(Reports); // Wrong place

【讨论】:

  • 对我来说似乎是正确的地方。 Async 设置为 false 并且 Reports 是一个变量,其范围超出了 jquery ajax 方法。
  • 如果我编码“Reports = json”,那么警报消息框中的输出是“[object Object],[object Object],[object Object]”
【解决方案3】:

我看到的第一件事是您没有序列化要返回的对象。你可以这样做

 return new JavaScriptSerializer().Serialize(your_object);

在客户端,您必须将 json 字符串转换为有效的 Js 对象,我在 json 响应字符串中使用“d”属性来做到这一点

var theObject = $.parseJSON(response.d);

并且 theObject 具有您需要的属性。

最后我看到你的对象是一个列表,你可以使用 $.each 进行迭代

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2013-11-04
    • 2012-02-02
    相关资源
    最近更新 更多