无法访问维基数据 API
Cannot access Wikidata API
我正在尝试使用维基数据 API,但我得到的只是:
Fetch API cannot load https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Ingmar%20Bergman&language=en&limit=20&format=json&origin=http%3A%2F%2Fwww.dev.example.com%3A3000. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.dev.example.com:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
这是代码:
const headers = new Headers();
const origin = "http://www.dev.example.com:3000";
headers.append("Origin", origin);
headers.append("Content-Type", "application/json; charset=UTF-8");
const url = "https://www.wikidata.org/w/api.php";
const query = {
action: "wbsearchentities",
search: "Ingmar Bergman",
language: "en",
limit: 20,
format: "json",
origin
};
const myInit = new Request(url + "?" + qs.stringify(query), {
method: "GET",
mode: "cors-with-forced-preflight",
headers
});
fetch(myInit)
.then(function(res) {
console.log(res);
})
.catch(function(err){
console.log(err);
});
我也试过JSONP,没成功。 运行 浏览器中的 link(只是没有 origin 参数)给出了正确的响应。
那么为什么要添加 "origin" 参数呢?将其关闭,或添加“&callback=some_function”以获得 JSONP。
将 origin=* 添加到查询字符串,例如
我正在尝试使用维基数据 API,但我得到的只是:
Fetch API cannot load https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Ingmar%20Bergman&language=en&limit=20&format=json&origin=http%3A%2F%2Fwww.dev.example.com%3A3000. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.dev.example.com:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
这是代码:
const headers = new Headers();
const origin = "http://www.dev.example.com:3000";
headers.append("Origin", origin);
headers.append("Content-Type", "application/json; charset=UTF-8");
const url = "https://www.wikidata.org/w/api.php";
const query = {
action: "wbsearchentities",
search: "Ingmar Bergman",
language: "en",
limit: 20,
format: "json",
origin
};
const myInit = new Request(url + "?" + qs.stringify(query), {
method: "GET",
mode: "cors-with-forced-preflight",
headers
});
fetch(myInit)
.then(function(res) {
console.log(res);
})
.catch(function(err){
console.log(err);
});
我也试过JSONP,没成功。 运行 浏览器中的 link(只是没有 origin 参数)给出了正确的响应。
那么为什么要添加 "origin" 参数呢?将其关闭,或添加“&callback=some_function”以获得 JSONP。
将 origin=* 添加到查询字符串,例如