获取维基百科令牌的问题
Problems with obtaining Wikipedia token
我正在尝试使用此代码获取维基百科编辑令牌:
$.getJSON("https://en.wikipedia.org/w/api.php?action=query&meta=tokens&format=json&callback=?", function (data) {
console.log( data );
});
但是,我收到此错误:
"Tokens may not be obtained when the same-origin policy is not applied."
使用 jQuery 获取令牌的正确方法是什么?
编辑: 我尝试了以下方法(基于评论):
- 删除回调=?并添加 origin=* - 抛出原始令牌错误
使用 here 中的修改示例:
$.ajax( {
url: 'https://en.wikipedia.org/w/api.php',
data: {
action: 'query',
meta: 'tokens',
format: 'json',
origin: 'http://example.com'
},
xhrFields: {
withCredentials: true
},
dataType: 'json'
}).done( function ( data ) {
console.log(data);
});
这个returns评论里提到的跨域错误
这是不可能的。
您真的希望 Internet 上任何地方的任何站点都能够使用您的 browser/IP address/Wikipedia 登录详细信息来编辑维基百科吗?我当然不知道。
Mediawiki(维基百科背后的软件)就是为了防止这种情况而设立的。基本上,您不能从不同的域执行任何 state-changing 操作(例如编辑、登录、任何其他更改任何内容)。您基本上可以只执行获取信息而不更改任何内容的请求。如果您 (a) 从特定 wiki 的域发出请求或 (b) wiki 配置为支持 您的特定服务器[=28=,则只能从浏览器使用 Mediawiki API ] 使用 CORS。
来自the API page on data formats:
When using JSON in callback mode, a number of things are disabled for security:
Tokens cannot be obtained (so state-changing actions aren't possible)
The client is treated as an anonymous user (i.e. not logged in) for all purposes, even after logging in through action=login
This means that things that require additional rights, such as rcprop=patrolled
, won't work unless anonymous users are allowed to use them
任何 state-changing 操作 (see "cross-site requests") 也无法使用 CORS 请求,除非为您的域专门启用。
我说的都是 "thank goodness"。
我正在尝试使用此代码获取维基百科编辑令牌:
$.getJSON("https://en.wikipedia.org/w/api.php?action=query&meta=tokens&format=json&callback=?", function (data) {
console.log( data );
});
但是,我收到此错误:
"Tokens may not be obtained when the same-origin policy is not applied."
使用 jQuery 获取令牌的正确方法是什么?
编辑: 我尝试了以下方法(基于评论):
- 删除回调=?并添加 origin=* - 抛出原始令牌错误
使用 here 中的修改示例:
$.ajax( { url: 'https://en.wikipedia.org/w/api.php', data: { action: 'query', meta: 'tokens', format: 'json', origin: 'http://example.com' }, xhrFields: { withCredentials: true }, dataType: 'json' }).done( function ( data ) { console.log(data); });
这个returns评论里提到的跨域错误
这是不可能的。
您真的希望 Internet 上任何地方的任何站点都能够使用您的 browser/IP address/Wikipedia 登录详细信息来编辑维基百科吗?我当然不知道。
Mediawiki(维基百科背后的软件)就是为了防止这种情况而设立的。基本上,您不能从不同的域执行任何 state-changing 操作(例如编辑、登录、任何其他更改任何内容)。您基本上可以只执行获取信息而不更改任何内容的请求。如果您 (a) 从特定 wiki 的域发出请求或 (b) wiki 配置为支持 您的特定服务器[=28=,则只能从浏览器使用 Mediawiki API ] 使用 CORS。
来自the API page on data formats:
When using JSON in callback mode, a number of things are disabled for security:
Tokens cannot be obtained (so state-changing actions aren't possible) The client is treated as an anonymous user (i.e. not logged in) for all purposes, even after logging in through
action=login
This means that things that require additional rights, such as
rcprop=patrolled
, won't work unless anonymous users are allowed to use them
任何 state-changing 操作 (see "cross-site requests") 也无法使用 CORS 请求,除非为您的域专门启用。
我说的都是 "thank goodness"。