$ .getJSON总是返回null

我试图做这个工作,但我不明白为什么这总是返回“请求失败”? json似乎是有效的(我知道jQuery对此严格),也许是因为httpS?

var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";

var jqxhr = $.getJSON(geo_url, {
    format: "json"
})
.done(function (json_data) {
    alert("currency: " + json_data.data.currency);
})
.fail(function () {
    alert("Request Failed");
});

var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";

$.ajax({
    url: geo_url,
    data: {
        format: "json"
    },
    crossDomain: true,
    dataType: 'jsonp',
    success: function (json_data) {
        alert("currency: " + json_data.data.currency);
        alert("city: " + json_data.data.city);
    },
    error: function () {
        alert("Request Failed");
    }
});

小提琴


请求应该使用jsonp来完成,因为跨域json-ajax请求是不允许的

$.ajax("https://spapi.cdnspstr.com/api/get_geo_ip",{
    dataType: 'jsonp',
    success: function(json_data) {
        alert("currency: " + json_data.data.currency);
    },
    error: function() {
        alert("Request Failed");
    }
});

如有疑问,请调试:

var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";

var jqxhr = $.getJSON(geo_url, {
    format: "json"
})
.done(function (json_data) {
    alert("currency: " + json_data.data.currency);
})
.fail(function (jqXHR, textStatus, errorThrown) {
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
    alert("Request Failed");
});

如果后端(500,403,...)出现任何错误,您仍然会收到Request Failed

链接地址: http://www.djcxy.com/p/47777.html

上一篇: $.getJSON always returns null

下一篇: CSRF prevention for GET requests