2015-03-08 79 views
-2

我現在很困惑,至於爲什麼這不起作用。爲什麼這個JSON查詢不起作用?

<div id="result" style="color:red"></div> 

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('https://www.eobot.com/api.aspx?coin=DOGE&json=true').then(function(data) { 
alert('Your Json result is: ' + data.DOGE); //you can comment this, i used it to debug 

result.innerText = data.DOGE; //display the result in an HTML element 
}, function(status) { //error detection.... 
    alert('Something went wrong.'); 
}); 

這是完全一樣的:http://jsfiddle.net/RamiSarieddine/HE2nY/1/但仍是工程..

編輯:爲了清楚起見,腳本應該採取的內容url(在DOGE元素內)並將其顯示在內

<div id="result" style="color:red"></div> 
+1

「not working?mean? – 2015-03-08 20:07:20

+0

@AndyRay它不輸出任何內容編輯:例如

保持爲空,它應該將url元素DOGE的內容放入它中。 – Ben 2015-03-08 20:09:52

回答

1

我測試了它,我得到了低於e rror,這意味着當您嘗試從https://www.eobot.com/api.aspx?coin=DOGE&json=true獲取json時,由於跨瀏覽器規則而阻止接收數據。

No 'Access-Control-Allow-Origin' header 

如何解決此問題,

您必須擁有https://www.eobot.comwww.eobot.com運行此腳本,否則你不能得到JSON由於跨域policy.Ask所有者白名單域在他的允許 - 起源標題。