【问题标题】:Why my GET request not working for Wikipedia API but working properly for any other API?为什么我的 GET 请求不适用于 Wikipedia API,但适用于任何其他 API?
【发布时间】:2016-11-16 13:07:07
【问题描述】:

我正在调用 Wikipedia opensearch API。 API url 是正确的,您可以在浏览器中打开它并获得正确的结果。但是当我通过同一个 url 请求时,它没有给出结果,预期的结果是 JSON 格式。我使用 RESTAPI 发送请求。我观察到,如果我使用任何其他 API,例如 http://ipinfo.io/json ,我会在 JSON 中得到正确的输出。但是我的代码对https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=ToBeSearchedText 没有任何作用。代码如下:

    $(document).ready(function() {

  //when the <submit> button is clicked
  $("button").click(function() {

    var xhr = new XMLHttpRequest();

    var bla = $("#searchitem").val(); //it will store the search query  text
    var url = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=" + bla;

    xhr.open("GET", url, false); //making a GET request
    xhr.send();

    var data = xhr.response; //response is stored in data

    $(".message").html(data); //this is doing nothing,if I try another url like http://ipinfo.io/json,the code works perfectly. Why it is not working for Wikipedia API?

  });

});

如果我能得到它,输出是下面的形式,这里的红色是搜索词,返回包含该词的维基百科文章列表:

[

    "Red",
    [
        "Red",
        "Redback spider",
        "Red panda",
        "Redshift",
        "Red Dwarf",
        "Red Hot Chili Peppers discography",
        "Red Star Belgrade",
        "Red hair",
        "Red Skull",
        "Reddit"
    ],
    [
        "Red is the color at the longer-wavelengths end of the spectrum of visible light next to orange, at the opposite end from violet.",
        "The redback spider (Latrodectus hasseltii) is a species of venomous spider indigenous to Australia. It is a member of the cosmopolitan genus Latrodectus, the widow spiders.",
        "The red panda' (Ailurus fulgens), also called the lesser panda, 'the red bear-cat, and the red cat-bear, is a mammal native to the eastern Himalayas and southwestern China.",
        "In physics, redshift happens when light or other electromagnetic radiation from an object is increased in wavelength, or shifted to the red end of the spectrum.",
        "Red Dwarf is a British comedy franchise which primarily comprises ten series (the ninth being a mini-series) of a television science fiction sitcom that aired on BBC Two between 1988 and 1993 and from 1997 to 1999, and on Dave in 2009 and 2012, gaining a cult following.",
        "The American rock band Red Hot Chili Peppers since 1984 has released eleven studio albums, three live albums, twelve compilation albums, eight video albums, five extended plays, forty-three singles, and forty-five music videos.",
        "Fudbalski klub Crvena Zvezda (Serbian Cyrillic: Фудбалски клуб Црвена Звезда, IPA: [t͡sř̩ʋenaː zʋěːzda]), commonly known in English as Red Star Belgrade (Serbian: Црвена Звезда Београд / Crvena Zvezda Beograd) or simply Red Star, is a Serbian professional football club based in Belgrade, the major part of the Red Star Sports Society.",
        "Red hair occurs naturally in 1–2% of the human population. It occurs more frequently (2–6%) in people of northern or western European ancestry, and less frequently in other populations.",
        "The Red Skull (Johann Schmidt) is a fictional supervillain appearing in American comic books published by Marvel Comics.",
        "Reddit (/ˈrɛdɪt/) is an entertainment, social news networking service, and news website. Reddit's registered community members can submit content, such as text posts or direct links."
    ],
    [
        "https://en.wikipedia.org/wiki/Red",
        "https://en.wikipedia.org/wiki/Redback_spider",
        "https://en.wikipedia.org/wiki/Red_panda",
        "https://en.wikipedia.org/wiki/Redshift",
        "https://en.wikipedia.org/wiki/Red_Dwarf",
        "https://en.wikipedia.org/wiki/Red_Hot_Chili_Peppers_discography",
        "https://en.wikipedia.org/wiki/Red_Star_Belgrade",
        "https://en.wikipedia.org/wiki/Red_hair",
        "https://en.wikipedia.org/wiki/Red_Skull",
        "https://en.wikipedia.org/wiki/Reddit"
    ]

]

但问题是我没有得到它,我试图通过 console.log(xhr.status); 检查状态,它什么也没做。这意味着没有数据被返回。请更正我的代码并帮助我理解我做错了什么?

这是我的 Codepen 的链接http://codepen.io/meow414/pen/kXxzzR

【问题讨论】:

标签: json api error-handling wikipedia-api


【解决方案1】:

您看不到任何响应加载的原因是浏览器强制执行同源策略。您正在尝试在当前位于 http 页面时发出 https 获取请求。另一个请求工作正常,因为它是一个 http 请求。

请参阅this 了解更多信息。

当你在那里时——因为你使用的是 jquery——你不必依赖 xmlhttprequest 本机对象。您可以将 jquery.get() 用于 GET 请求或使用支持所有 HTTP 动词的 $.ajax()。

希望有帮助!

【讨论】:

    【解决方案2】:

    https://www.mediawiki.org/wiki/Manual:CORS

    这是特定于在 mediawiki 中使用 CORS 的。对于匿名执行的搜索,请在 url 中包含 &amp;origin=*

    https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search=
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 2021-09-06
      • 1970-01-01
      • 2019-07-03
      • 2020-06-13
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多