使用WikipediaAPI中的jQuery Json获取城市信息
这是我想从中获取信息的URL 我想抓取使用WikipediaAPI中的jQuery Json获取城市信息,jquery,Jquery,这是我想从中获取信息的URL 我想抓取摘录 这是我的代码,以波士顿为例 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); }) 但
摘录
这是我的代码,以波士顿为例
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);
})
但控制台中没有显示任何内容,即使我更改了数据。将提取为一些随机文本
我现在得到这个错误
无法加载XMLHttpRequest
.
请求的服务器上不存在“Access Control Allow Origin”标头
资源。因此,不允许访问源“null”
如果有人感兴趣,我就解决了
$(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);
});
});
});
您确定存在提取属性吗。当您仅使用console.log数据时会发生什么情况?是的,我确信有一个extract
属性,您可以访问该URL并确保您自己。当我只输出数据时,什么也没有发生。您是否收到错误?我正在获取无法加载的XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。这是有意义的,因为您正在尝试执行跨站点requestextract可以通过以下方式访问:data.query.pages.extract如果是json。。。但通常从@mathieudelimier中提取信息会更好,你是对的。经过一些修改后,我得到了这个错误XMLHttpRequest无法加载https://en.wikipedia.org/w/api.php?. 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“null”