无法执行来自维基百科的Ajax请求 [英] Cannot carry out Ajax request from wikipedia

查看:52
本文介绍了无法执行来自维基百科的Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用Wikipedia API来获取人物图像,但是失败了.我使用以下网址: https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Albert%20Einstein&pithumbsize=100 当我的控制台浏览器显示以下内容

I want to use wikipedia API in my project to grab images of people, but fail. I use this url:https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Albert%20Einstein&pithumbsize=100 When i console browser says the following

Refused to execute script from 'https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Albe…Callback&callback=jQuery22409288979864744966_1470068280411&_=1470068280412' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

我的代码

var general = {
    // The URL to the quote API
    url: 'http://api.forismatic.com/api/1.0/',
    // What to display as the author name if s/he's unknown
    unknownAuthor: 'Uknown',
    // Base URL for the tweet links generation
    tweetURL: 'http://twitter.com/home?status=',
    wikiURL:'https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Albert Einstein&pithumbsize=100&callback=wikiCallback'
};

var wikirequest = function() {
    $.ajax({
      url:general.wikiURL,
      dataType: 'jsonp',
      success: function(wikData) {
      console.log(wikData);
      //var image = wikiData.
      displayQuote(image);
      } // end of success
    });
}// wikirequest

wikirequest();

有人遇到过同样的问题吗?

Has anyone met the same issue?

推荐答案

您正尝试使用JSONP加载数据,但是您正在向返回HTML文档的URL发出请求.必须使用JavaScript程序来回答JSONP请求(因为这是它们如何工作的基本特征……以及为什么它们很危险,应避免使用纯JSON和CORS来避免).

You are trying to load the data using JSONP, but you are making a request to a URL that returns an HTML document. JSONP requests have to be answered with JavaScript programs (since that is a fundamental feature of how they work … and also why they are dangerous and should be avoided in favour of plain JSON and CORS).

要使其返回JSONP,您需要提供两个附加的查询字符串参数:

To make it return JSONP you need to provided two additional query string parameters:

  • format = json
  • callback = YourCallbackName

…其中, YourCallbackName 是应执行的函数的名称,并将要获取的数据作为参数传递给该函数.当您指定 callback =?时,大多数Ajax库都会动态生成该名称(以及函数本身).

… where YourCallbackName is the name of the function that should be executed and passed the data you are fetching as an argument. Most Ajax libraries will generate that name (and the function itself) dynamically when you specify callback=?.

这篇关于无法执行来自维基百科的Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆