-1

我希望 jquery 从我的 MVC 控制器中获取 JsonResult,但它没有收到任何数据!

如果我将输出放入一个文本文件并输入它的链接它的工作,所以我认为我的 jQuery 很好。

然后我正在使用其他浏览器(如 chrome)进行测试,但我什么也没看到。请求的页面只是 emtpy.. 没有错误。此外,IE 似乎在接收我的字符串时遇到问题。只有 firefox 显示字符串,但为什么呢?

    public JsonResult jsonLastRequests()
    {
        List<Request> requests = new List<Request>();
        while (r.Read())
        {
            requests.Add(new Models.Request()
            {
                ID = (int)r[0],
                SiteID = r[1].ToString(),
                Lat = r[2].ToString(),
                City = r[4].ToString(),
                CreationTime = (DateTime)r[5]
            });
        }
        r.Close();
        return Json(requests);
    }

我发现如果我想将 JSON 作为字符串返回它也不起作用!它现在在所有浏览器中都可以使用字符串.. 但是 jQuery 仍然没有加载任何东西

        var url = "http://../jsonLastRequests";
        var source =
        {
            datatype: "json",
            datafields: [
                { name: 'ID' },
                { name: 'SiteID' },
                { name: 'Lat' },
                { name: 'CreationTime' },
                { name: 'City' },
            ],
            id: 'id',
            url: url
        };
        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function (data, status, xhr) { },
            loadComplete: function (data) { },
            loadError: function (xhr, status, error) { }
        });

我通过添加解决了我的问题: access-control-allow-origin:*

4

1 回答 1

1
 public HtmlString jsonLastRequests()
    {
        List<Request> requests = new List<Request>();
        while (r.Read())
        {
            requests.Add(new Models.Request()
            {
                ID = (int)r[0],
                SiteID = r[1].ToString(),
                Lat = r[2].ToString(),
                City = r[4].ToString(),
                CreationTime = (DateTime)r[5]
            });
        }  r.Close();
 System.Web.Script.Serialization.JavaScriptSerializer jSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();


            return new HtmlString(jSerializer.Serialize(requests ));}

我做了同样的方法

  $.ajax({
        type: 'POST',
        url: '/home/GetSurvey',
        data: {
            XmlPath: $("#xmlpath").val()
        },
        dataType: 'json',
        success: function (jsonData) {
            jsonStringQuestionaire = jsonData;
            LoadSurvey();
        },
        error: function () {
            alert('Error loading ' + id);
        }
    });
 questionaireJsonList = eval(jsonStringQuestionaire);
于 2012-09-04T15:08:00.077 回答