Javascript 使用ajax从WikipediaAPI渲染图像
我试图呈现维基百科文章的图像 例如从 为了避免跨源策略,我使用代理:Javascript 使用ajax从WikipediaAPI渲染图像,javascript,jquery,ajax,api,wikipedia,Javascript,Jquery,Ajax,Api,Wikipedia,我试图呈现维基百科文章的图像 例如从 为了避免跨源策略,我使用代理: function imageWp() { var word = 'Appendizitis'; $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var https = (window.location.protocol === 'http:' ? 'http:' : '
function imageWp() {
var word = 'Appendizitis';
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var https = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = https + '//cors-anywhere.herokuapp.com/' + options.url;
}
});
$.get(
'https://de.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=' + word + '&callback=?',
function (response) {
console.log("> ", response);
$("#viewer").html(response);
});
}
如何仅解析图像?
或者,有没有更好的方式使用javascript、jquery和ajax?我不想使用PHP。我会使用正则表达式从响应中提取我想要的内容 大概是这样的:
$.get(
'https://de.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=' + word + '&callback=?',
function (response) {
var resp = $.parseJSON(response);
var text = resp.parse.text['*'];
re = /img.*?src="(.*?)"/g
while( match = re.exec(text)) {
console.log(match[1]);
}
});
在match[1]中,您将拥有图像的“src”属性。这段代码并不完美,但它应该告诉你我的意思
函数imageWp(){
var word=‘阑尾炎’;
$.ajaxPrefilter(函数(选项){
if(options.crossDomain&&jQuery.support.cors){
var https=(window.location.protocol=='http:'?'http:':'https:');
options.url=https+'//cors anywhere.herokuapp.com/'+options.url;
}
});
美元(
'https://de.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=“+word+”&回调=?”,
功能(响应){
var-m;
var url=[];
var regex=/');
});
});
}
imageWp()代码>
查看维基百科API:
您可以查询文章并获取JSON格式的图像列表:
{
"warnings": {
"query": {
"*": "..."
}
},
"query-continue": {
"images": {
"imcontinue": "70980|Stomach_colon_rectum_diagram-en.svg"
}
},
"query": {
"pages": {
"70980": {
"pageid": 70980,
"ns": 0,
"title": "Appendicitis",
"images": [
{
"ns": 6,
"title": "File:Acute Appendicitis.jpg"
},
{
"ns": 6,
"title": "File:Acute appendicitis High Power.jpg"
},
{
"ns": 6,
"title": "File:Appendicitis - low mag.jpg"
},
{
"ns": 6,
"title": "File:Appendicitis world map - DALY - WHO2004.svg"
},
{
"ns": 6,
"title": "File:Appendix-Entfernung.jpg"
},
{
"ns": 6,
"title": "File:CAT scan demonstrating acute appendicitis.jpg"
},
{
"ns": 6,
"title": "File:Commons-logo.svg"
},
{
"ns": 6,
"title": "File:Inflamed appendix.jpg"
},
{
"ns": 6,
"title": "File:SonoAppendizitis.JPG"
},
{
"ns": 6,
"title": "File:Stitches post appendicitis surgery.jpg"
}
]
}
}
}
}