的javascript
我的頁面內需要下載駐留在以下位置的小文本文件(只是一個小JSON陣列):如何用JavaScript下載THIS文件?
http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/obterPosicoesDaLinha/410
MIME類型的文檔是application/json
。 我試着用XMLHttpRequest
,但我得到了一個錯誤:
XMLHttpRequest cannot load http://dados[...]/410. No 'Access-Control-Allow-Origin' header
is present on the requested resource. Origin 'null' is therefore not allowed access.
我GOOGLE了這一點,並在解決方案指出CORS和改變在服務器端的東西,這是我不能做。
有沒有什麼辦法可以用javascript(也只是javascript)檢索這個內容?
謝謝! L.
編輯
繼@naresh建議,我與JSONP嘗試。我添加這些行到我的網頁,但沒有任何反應(甚至不是一個控制檯錯誤):
var source = "http://dados[...]/409";
script = document.createElement('script');
script.type = 'text/javascript';
script.src = source + '?callback=downloadLinha';
document.body.appendChild(script);
我的功能downloadLinha(data)
只是alert(data)
。
EDIT 2
我聯繫了服務器管理員,並且,出乎我的意料,他們在幾個小時內修復了這個問題!我沒想到他們甚至會回答。所以我的實際問題解決了,但是如果沒有管理員的干預,我就找不到答案。
無論如何,非常感謝所有試圖幫助的人!
**「否‘訪問控制允許來源’頭 出現在請求的資源上,因此不允許Origin'null'訪問。「** –
您也可以使用JSONP。見http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about – naresh
http://en.wikipedia.org/wiki/Same-origin_policy – SLaks