javascript - 无法访问维基数据 API

标签 javascript cors mediawiki-api wikidata 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,但没有成功。在浏览器中运行链接(只是没有 origin 参数)会给出正确的响应。

最佳答案

那么为什么要添加“origin”参数呢?将其关闭,或添加“&callback=some_function”以获得 JSONP。

关于javascript - 无法访问维基数据 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478250/

相关文章:

javascript - 使用下划线随机选择并从集合中删除

javascript - 如何将子 props 与传入的 Parent 状态断开?

php - 在 MediaWiki 搜索中指定偏移量

search - 维基百科 api 搜索标题生成器

javascript - 将用户事件存储在客户端数据库中

javascript - 如何在没有 _observer "magic"_(使用 vuecharts.js)的情况下将可变数量的数据集推送到图表数据?

node.js - 无论我将其添加到何处,Azure 应用程序服务( Node )CORS 源都无法工作

node.js - 无法为 Express.js 应用程序 (CORS) 设置 header

javascript - 为什么 Access-Control-Allow-Origin 在 Codeigniter Rest-Server 中不起作用

php - 如何使用 API 获取带有绝对 URL 的维基百科页面 HTML?