Wikipedia API支持CORS还是仅支持JSONP? [英] Does Wikipedia API support CORS or only JSONP available?
问题描述
这个问题与另一个问题相关,这是一年前提出的问题。作者询问如何使用JavaScript和维基百科API创建跨网域请求,其中一个注释是:
This question related to another question, which was asked year ago. Author asked how to make cros-origin request using JavaScript and Wikipedia API and one comment was:
en.wikipedia.org允许CORS
en.wikipedia.org doesn't seem to allow CORS
,他建议使用JSONP。
and he was advised to use JSONP instead.
我知道我可以使用JSONP,但我更喜欢CORS,如果我可以使用它。
I know I can use JSONP, but I prefer CORS if I can use it.
我试过 jsfiddle
var url = "https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json";
$.ajax({
url: url,
data: 'query',
dataType: 'json',
type: 'POST',
headers: { 'Api-User-Agent': 'Example/1.0' },
origin: 'https://jsfiddle.net/',
success: function (data) {
console.log(data);
//do something with data
}});
并获得以下错误:
XMLHttpRequest无法加载
https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json 。
对预检请求的响应不通过访问控制检查:否
在请求的
资源上存在Access-Control-Allow-Origin头。原因 https://fiddle.jshell.net 因此不允许
访问。
XMLHttpRequest cannot load https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.
请求标题:
authority:en.wikipedia.org
method:OPTIONS
path:/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json
scheme:https
accept:/
accept-encoding:gzip, deflate, sdch
accept-language:en-US,en;q=0.8,fr-CA;q=0.6,fr;q=0.4,fr-FR;q=0.2,ru;q=0.2,uk;q=0.2
access-control-request-headers:accept, api-user-agent, content-type
access-control-request-method:POST
origin:https://fiddle.jshell.net
referer:https://fiddle.jshell.net/_display/
user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
b $ b
响应标题:
Response Header:
accept-ranges:bytes
age:0
backend-timing:D=33198 t=1462749020308717
cache-control:no-cache
content-encoding:gzip
content-length:20
content-type:text/html
date:Sun, 08 May 2016 23:10:20 GMT
p3p:CP="This is not a P3P policy! See https://en.wikipedia.org/wiki/Special:CentralAutoLogin/P3P for more info."
server:mw1114.eqiad.wmnet
set-cookie:CP=H2; Path=/; secure
set-cookie:GeoIP=US:MA:Waltham:42.37:-71.24:v4; Path=/; secure; Domain=.wikipedia.org
set-cookie:WMF-Last-Access=08-May-2016;Path=/;HttpOnly;secure;
Expires=Thu, 09 Jun 2016 12:00:00 GMT
status:200
strict-transport-security:max-age=31536000; includeSubDomains; preload
vary:Accept-Encoding
via:1.1 varnish, 1.1 varnish
x-analytics:https=1;nocookies=1
x-cache:cp1066 pass+chfp(0), cp1055 frontend pass+chfp(0)
x-client-ip:146.115.167.51
x-content-type-options:nosniff
x-powered-by:HHVM/3.12.1
x-varnish:2807049448, 2537048470
需要确认CORS不适用于Wikipedia API,我需要使用JSONP。
So, I need confirmation that CORS doesn't work for Wikipedia API and I need use JSONP.
推荐答案
请参阅在API的JSON响应中启用跨域API请求,这是维基媒体网站的一个开放式错误,表明它们当前仅支持来自不同的CORS请求维基媒体网站本身到其他维基媒体网站,但他们不支持来自外部网站的CORS请求。
See "Enable cross-domain API requests in API's JSON responses", an open bug for Wikimedia sites that indicates that they currently only support CORS requests from different Wikimedia sites themselves to other Wikimedia sites—but they do not support CORS requests from external sites.
特别参见 https://phabricator.wikimedia.org/T62835#2191138 (从2016年4月8日起),这是一个摘要,表明他们正在考虑更改允许来自外部网站的CORS请求,但他们尚未启用。
See in particular https://phabricator.wikimedia.org/T62835#2191138 (from Apr 8, 2016) which is a summary that indicates they are considering to make a change to allow CORS request from external sites, but they have not yet enabled it.
来触发新的行为,你需要在你的url params中指定
origin = *
。这个项目目前被隐藏在 T62835 讨论中,并未在文档。
to trigger the new behaviour, you need to specify
origin=*
in your url params. This is currently buried in the T62835 discussion and is not stated in the documentation yet.
更新时间:2016-07-12
a href =https://phabricator.wikimedia.org/T62835#2447638 =nofollow>今天部署CORS支持:
未经身份验证的跨网域API请求。这个
应该部署到WMF维基与1.128.0-wmf.10,请参阅
https://www.mediawiki.org/wiki/MediaWiki_1.28/Roadmap 的时间表
https://www.mediawiki.org/wiki/MediaWiki_1.28/Roadmap表示1.128.0-wmf.10的部署日期为2016年7月12日至2016年7月14日。
https://www.mediawiki.org/wiki/MediaWiki_1.28/Roadmap indicates the 1.128.0-wmf.10 deployment dates are 12 July 2016 to 14 July 2016.
这篇关于Wikipedia API支持CORS还是仅支持JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!