【问题标题】:Grab city info using jQuery Json from Wikipedia api使用 Wikipedia api 中的 jQuery Json 获取城市信息
【发布时间】:2016-07-12 05:32:39
【问题描述】:

这是我想从中获取信息的 URL

https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=boston&format=json&formatversion=2&exintro=1

我要抢extract

这是我的代码,以Boston 为例

var jqxhr = $.getJSON( "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=boston&format=json&formatversion=2&exintro=1", function(data) {
    console.log(data.extract);
})

但控制台中什么也没有显示,即使我将 data.extract 更改为一些随机文本。

我现在收到此错误

XMLHttpRequest 无法加载 https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=boston&format=json&formatversion=2&exintro=1。 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此,不允许访问 Origin 'null'。

【问题讨论】:

  • 您确定存在提取属性吗?当你 console.log 只是数据时会发生什么?
  • 是的,我确定有一个 extract 属性,您可以访问该 URL 并确保自己。当我只输出 data 时没有任何反应
  • 您遇到错误了吗?我收到 XMLHttpRequest 无法加载 en.wikipedia.org/w/…。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'stackoverflow.com' 不允许访问。这是有道理的,因为您正在尝试执行跨站点请求
  • extract 可以通过以下方式访问:data.query.pages.extract 如果是 json...但通常最好从 wiki.dbpedia.org 中提取信息
  • @MathieudeLorimier 你是对的。经过一番摆弄后,我收到了这个错误XMLHttpRequest cannot load https://en.wikipedia.org/w/api.php?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

标签: jquery


【解决方案1】:

如果有人感兴趣,我已经解决了

$(document).ready(function(){
        var CityURL = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=boston&format=json&formatversion=2&exintro=1&callback=?';

        $.getJSON(CityURL ,function(data) {
            $.each(data.query.pages, function(i, item) {
                console.log(item.extract);

                $('div#details').html(item.extract);
            });
        });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多