javascript jquery ajax 成功结果为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3347462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:54:59  来源:igfitidea点击:

jquery ajax success result is null

javascriptjqueryodata

提问by Rush Frisby

I am doing an ajax call using jquery to get data in json format. the success callback function is called but the data is empty.

我正在使用 jquery 进行 ajax 调用以获取 json 格式的数据。调用成功回调函数但数据为空。

$(document).ready(function () {
    $.ajax({
        url: "http://apps.sungardhe.com/StudentResearch/public/Research.svc/Schools",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: cbSchools
    });
});

function cbSchools(data) {
    if (data == null) {
        alert("data is null");
        return;
    }
    for (var school in data) {
        $("#ddSchool").append("<option value='" + data[school].ShortName + "'>" + data[school].ShortName + "</option>");
    }
}

using fiddler I see that the response is actually returning the json data but for some reason the jquery result object is null. can anyone tell me why?

使用提琴手,我看到响应实际上返回了 json 数据,但由于某种原因,jquery 结果对象为空。谁能告诉我为什么?

回答by Nick Craver

You're being blocked by the same-origin policywhich prevents cross-domain XMLHttpRequests. Since you need to set headers to get JSON back from a .Net web service like this, you're in a tough spot, you just can't make that kind of request from a browser, not from a different domain.

您被阻止跨域 XMLHttpRequests的同源策略阻止。由于您需要设置标头以从这样的 .Net Web 服务中获取 JSON,因此您处于困境,您不能从浏览器发出这种请求,而不是从不同的域发出这种请求。

Fiddler may be showing the content, but the browser isn't going to let the page see it, for security reasons it'll always be null. The one way around this is JSONP, but unfortunately it doesn't look like that service is setup to support it.

Fiddler 可能正在显示内容,但浏览器不会让页面看到它,出于安全原因,它始终为空。解决此问题的一种方法是JSONP,但不幸的是,该服务似乎并未设置为支持它。

回答by Mark Schultheiss

I believe you can make your calls generic (reason as marduk indicates)

我相信你可以让你的电话通用(原因正如马尔杜克所指出的)

To handle this, and to make calls generic (works with data and data.d), I use the following in my ajax calls (with my asp.net stuff) so that it works with older as well as newer services:

为了处理这个问题,并使调用通用(适用于数据和 data.d),我在我的 ajax 调用中使用以下内容(使用我的 asp.net 内容),以便它适用于较旧和较新的服务:

   dataFilter: function(data)
    {
        var msg;
        if (typeof (JSON) !== 'undefined' &&
        typeof (JSON.parse) === 'function')
            msg = JSON.parse(data);
        else
            msg = eval('(' + data + ')');
        if (msg.hasOwnProperty('d'))
            return msg.d;
        else
            return msg;
    },

EDIT: IF it is truly null AND NOT "undefined" then the cross domain issue might be in play here.

编辑:如果它真的为空并且不是“未定义”,那么跨域问题可能在这里起作用。

回答by gehsekky

try this

试试这个

if (data.d == null) {
    alert("data.d is null");
    return;
}

since your return datatype is json, the data is in the data, "d", variable in the response object.

由于您的返回数据类型是 json,因此数据位于响应对象中的数据“d”变量中。