2013-10-29 55 views
0

我正在一個項目,我必須做一個jQuery調用LSD Finder。澄清,在這種情況下,LSD不是毒品:它是土地子部門。加拿大被拆分成數百個,如果不是數千塊土地,他們都有一個數字,比如:14-22-25-02-W5。沒關係,這對你來說並不意味着什麼,有一個API。jQuery AJAX Failedly,沒有想法爲什麼

但是對於一些愚蠢的原因,我無法獲得該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,我不斷收到「無」。我試圖得到一個更智能的錯誤,就像modifiying錯誤回調的響應文本或東西閱讀以下內容:

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)" 
    } 
} 
] 

我想要做什麼,最終是讓這個查詢工作,然後得到的緯度和經度,並用它在一個動態的谷歌地圖做了點。但首先,我需要讓查詢與我一起工作。我認爲這個問題可能在於我對一個https地址有興趣,但現在是凌晨3點30分,我不能直視,所以我希望有人有一個想法。

謝謝。

+1

你能舉一個「價值」的例子嗎?沒關係,我只注意到你不想公開你的鑰匙。如果將完整的網址粘貼到瀏覽器中會發生什麼情況? – Johan

+0

價值的一個例子是「14-22-25-02-W5」。我會分享鑰匙,但我必須付出代價:(如果我粘貼完整的網址到我的瀏覽器中,我得到上面的json數組,它確實驗證。 –

+0

什麼是您的服務器端語言? – Johan

回答

2

jQuery Ajax默認不支持跨域查詢。嘗試將crossDomain: true添加到您的請求選項。

+0

+1服務器還需要提供JSONP支持JSONP工作 –

+0

我最終對本地文件進行了查詢,該文件對API做了cURL並得到了結果,這是crossDomain的問題,我從來沒有弄明白,但我以另一種方式工作,但謝謝你的回答:) –

0

看來你正在訪問不在你的服務器上的東西。

您需要使用COARSE。

相關問題