【发布时间】:2018-01-03 18:58:37
【问题描述】:
我想从 API 获取 json 数据,但它似乎不起作用。我认为代码会自己说话
var URL = 'https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en';
getData(URL);
function getData(source){
xhr = new XMLHttpRequest();
xhr.onload = function(){
if(xhr.status === 200){
data = JSON.parse(xhr.responseText);
console.log(data);
}
}
xhr.open('GET', source, true);
xhr.send(null);
}
此代码给出控制台错误Failed to load https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
我怀疑它可能是 CORS,但我不知道如何修复它。
【问题讨论】:
标签: javascript json ajax