javascript - jQuery AJAX 不断失败,不知道为什么

标签 javascript jquery ajax json

我正在开发一个项目,我必须对 LSD Finder 进行 jQuery 调用。澄清一下,在这种情况下,LSD 不是药物:它是 Land Sub Divisions。加拿大被分成数百 block 甚至数千 block 土地,它们都有一个编号,例如:14-22-25-02-W5。没关系,这对您来说没有任何意义,有一个 API 可以实现这一点。

但是由于一些愚蠢的原因,我无法让该 API 工作。我的 jQuery AJAX 查询很简单:

//the variable 'value' comes from an input, and is correct
var url = "https://www.lsdfinder.com/api/v1/SECRETKEY/lsd/"+value;

$.ajax({
    url: url,
dataType: 'json',
success: function(json) {
    alert('anything?');
},
    error: function(e) {
    alert('nothing');
}
});

无论我执行 dataType json 还是 jsonp 似乎都不重要,我总是得到“什么也没有”。我尝试通过修改错误回调来读取以下内容来获取更智能的错误,例如响应文本或其他内容:

error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.toSource());
alert(textStatus.toSource());
alert(errorThrown.toSource());
}

我的回答是,包括原始的误差函数:

nothing

//deep breath, and....
({readyState:0, setRequestHeader:(function (a,b){if(!v){var c=a.toLowerCase();a=u[c]=u[c]||a,t[a]=b}return this}), getAllResponseHeaders:(function (){return v===2?e:null}), getResponseHeader:(function (a){var c;if(v===2){if(!f){f={};while(c=cl.exec(e))f[c[1].toLowerCase()]=c[2]}c=f[a.toLowerCase()]}return c===b?null:c}), overrideMimeType:(function (a){return v||(l.mimeType=a),this}), abort:(function (a){return a=a||w,g&&g.abort(a),y(0,a),this}), state:(function (){return c}), always:(function (){return e.done(arguments).fail(arguments),this}), then:(function (){var a=arguments;return p.Deferred(function(c){p.each(b,function(b,d){var f=d[0],g=a[b];e[d[1]](p.isFunction(g)?function(){var a=g.apply(this,arguments);a&&p.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f+"With"](this===e?c:this,[a])}:c[f])}),a=null}).promise()}), promise:(function (a){return typeof a=="object"?p.extend(a,d):d}), pipe:(function (){var a=arguments;return p.Deferred(function(c){p.each(b,function(b,d){var f=d[0],g=a[b];e[d[1]](p.isFunction(g)?function(){var a=g.apply(this,arguments);a&&p.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f+"With"](this===e?c:this,[a])}:c[f])}),a=null}).promise()}), done:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), fail:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), progress:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), success:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), error:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), complete:(function (){if(i){var b=i.length;(function d(b){p.each(b,function(b,c){p.isFunction(c)&&(!a.unique||!l.has(c))?i.push(c):c&&c.length&&d(c)})})(arguments),e?g=i.length:c&&(f=b,k(c))}return this}), statusCode:(function (a){if(a){var b;if(v<2)for(b in a)r[b]=[r[b],a[b]];else b=a[x.status],x.always(b)}return this}), responseText:"", status:0, statusText:"error"})

new String("error")

new String("")

所以,正如你所看到的,它 100% 失败,我不知道为什么。我认为我返回的 JSON 数组可能不正确或已损坏,因此我获取了它并通过 JSONLint 运行它,它说它是有效的。只是为了让您可以看到它,它是:

[
{
    "query": "14-22-25-02-W5",
    "response": {
        "status": "ok",
        "err": [],
        "lat": 51.152259,
        "lng": -114.202199,
        "country": "Canada",
        "province": "AB",
        "city": "Calgary",
        "street": "49 Royal Vista Drive NW",
        "street_prox": 78,
        "address": "49 Royal Vista Drive NW, Calgary, AB",
        "lsd": "14-22-25-2 W5",
        "lsd_border": [
            [
                51.150459,
                -114.199327
            ],
            [
                51.150447,
                -114.205067
            ],
            [
                51.154059,
                -114.205071
            ],
            [
                51.154072,
                -114.199332
            ],
            [
                51.150459,
                -114.199327
            ]
        ],
        "uwi": "",
        "nts": "",
        "nts_border": [],
        "utm": "11S 695661E 15670479N",
        "utm_v": "Zone 11, 695661 meters easting, 15670479 meters northing (Southern Hemisphere)"
    }
}
]

最终,我想做的是让这个查询工作,然后获取纬度和经度,并用它在动态 Google map 上指出一个点。但首先,我需要让查询与我一起工作。我认为问题可能在于我正在 ajax 到一个 https 地址,但现在是凌晨 3:30,我无法正常思考,所以我希望有人有一个想法。

谢谢。

最佳答案

jQuery Ajax 默认不支持跨域查询。尝试将 crossDomain: true 添加到您的请求选项中。

关于javascript - jQuery AJAX 不断失败,不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654300/

相关文章:

Chrome 72 更新后 Android Cordova 应用渲染缓慢

javascript - 如何将数据从 JSONP 转换为 JSON

javascript - 什么是 window.localStorage

javascript - BeautifulSoup 获取 innerhtml 数据

jquery - CSS Transitions 在 toggleClass() 之后不工作

javascript - 如何让图片库部分显示 "AJAX Loading.gif"? (jQuery)

javascript - 如何确定最后一个 tabs.onRemoved 在 Firefox WebExtensions 中实际触发?

javascript - Angular-需要ngModel并将其用于自定义指令的 Controller 中,而不是链接函数中

javascript - 哪个控件触发了该事件

jquery - 为什么当我使用 jQuery 自定义插件将 src 添加到图像时,该段落被下移了?