我正在尝试使用POST XMLHttpRequest从wikidata API获取结果,以获取查询结果。但是只有少数几个请求通过,有些返回CORS发出错误,这很令人困惑。
我的请求如下所示,据wikidata文档了解,我已经在url本身中设置了origin参数。我也尝试过在标头中设置原点,这也行不通。
setTimeout(function () {
var xhr = new XMLHttpRequest();
xhr.open(
"POST","https://query.wikidata.org/sparql?origin=*&format=json&query=" +
encodeURIComponent(
queryService(settingsService()).getPropObject(
vm.selected.uri,prop
)
),true
);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
let data = JSON.parse(xhr.response);
setObjectInnerHtml(label,data,prop);
}
running -= 1;
};
xhr.send();
},300);
但是它在xhr.send()处返回错误,如下所示:
access to XMLHttpRequest at 'https://query.wikidata.org/sparql?origin=*&format=json&query=PREFIX%20rdf%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0ASELECT%20DISTINCT%20%3Furi%20%3FuriLabel%20WHERE%20%7B%0A%20%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ183%3E%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2FP1889%3E%20%3Furi%20.%0A%20%20OPTIONAL%20%7B%20%3Furi%20rdfs%3Alabel%20%3FuriLabel%20.%20FILTER%20(lang(%3FuriLabel)%20%3D%20%22en%22)%7D%0A%7D' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'access-control-allow-origin' header is present on the requested resource.
这里可能出什么问题了?
更新 这是来自同一方法的一系列请求,可以看出其中有些请求通过,有些则没有。引发的错误是CORS问题。