我希望能夠從小頁面讀取信息。如何從外部URL獲取JSON數據
我有一個顯示以下信息的JSON服務的地址:
,我希望我可以保持顯示的數字。
我測試了這個example並正常工作,但是當我嘗試使用我的URL時,沒有任何反應。我不知道我是否能夠正確理解問題,但我希望有人能夠幫助我。
如果有任何問題,我儘量做出最好的解釋。
我現在問不便道歉。
,我用
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON('http://MYADDRESS/json.do?_ULN[1]').then(function(data) {
alert('Your Json result is: ' + data.result); //you can comment this, i used it to debug
result.innerText = data.result; //display the result in an HTML element
}, function(status) { //error detection....
alert('Something went wrong.');
});
控制檯中的任何錯誤? – phts 2015-04-01 10:45:55
假設您的請求因跨域限制而不起作用 – phts 2015-04-01 10:47:06
感謝您的回覆。只是沒有出現。如果我用https嘗試使用錯誤消息。但是,如果我只用http嘗試,不會給我什麼。 – rpirez 2015-04-01 10:55:32