使用Ajax检索MediaWiki完整图像URL

发布于 2025-01-21 04:17:25 字数 613 浏览 3 评论 0 原文

MediaWiki的URL可直接直接映射URL。

<img src=
      "https://www.mediawiki.org/wiki/Special:FilePath/Wikimedia_Hackathon_Prague_2019_-_Group_Photo_-_CLK_-_cropped.jpg"/>

但是,使用此路径的问题是,有时图像将无法加载。我的猜测是,URL正在经过多个重定向以获取实际图像的URL。如果您尝试以相同的方式加载多个图像,这尤其会变得更加有问题。因此,我试图查看是否可以使用Ajax检索特殊的最终路径:filepath URL。

$.ajax({
    url:
      "https://www.mediawiki.org/wiki/Special:FilePath/Wikimedia_Hackathon_Prague_2019_-_Group_Photo_-_CLK_-_cropped.jpg",
    type: "GET",
    success: function (data) {
      console.log(data);
    }
  });

但是,这甚至从未取得成功,所以我不确定需要发生什么。

MediaWiki has a url that directs to the full image url.

<img src=
      "https://www.mediawiki.org/wiki/Special:FilePath/Wikimedia_Hackathon_Prague_2019_-_Group_Photo_-_CLK_-_cropped.jpg"/>

However, the issue with using this path is that sometimes the image will fail to load. My guess is that the url is going through several redirects to fetch the actual image's url. This especially becomes more problematic if you're trying to load multiple images at the same. As such, I'm trying to see if I can retrieve the final path of the Special:FilePath url using ajax.

$.ajax({
    url:
      "https://www.mediawiki.org/wiki/Special:FilePath/Wikimedia_Hackathon_Prague_2019_-_Group_Photo_-_CLK_-_cropped.jpg",
    type: "GET",
    success: function (data) {
      console.log(data);
    }
  });

However, this never even reaches success, so I'm not certain what needs to happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2025-01-28 04:17:25

如果要使用ajax,请尝试 Mediawiki api 手动):

通用JavaScript:

var api = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    format: "json",
    prop: "imageinfo",
    iiprop: "url",
    titles: "File:Wikimedia Hackathon Prague 2019 - Group Photo - CLK - cropped.jpg"
};

api = api + "?origin=*";
Object.keys(params).forEach(function(key){api += "&" + key + "=" + params[key];});

fetch(api)
    .then(function(response){return response.json();})
    .then(function(response) {
        var pages = response.query.pages;
        for (var p in pages) {
            console.log(pages[p].title + " is at: " + pages[p].imageinfo[0].url);
        }
    })
    .catch(function(error){console.log(error);});

Mediawiki 环境中:

var params = {
    action: 'query',
    format: 'json',
    prop: 'imageinfo',
    iiprop: 'url',
    titles: 'File:Wikimedia Hackathon Prague 2019 - Group Photo - CLK - cropped.jpg'
},
api = new mw.Api();

api.get( params ).done( function ( data ) {
    var pages = data.query.pages,
        p;
    for ( p in pages ) {
        console.log( pages[p].title + ' is at: ' + pages[p].imageinfo[0].url );
    }
});

If you want to use AJAX, try MediaWiki API (manual):

Generic JavaScript:

var api = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    format: "json",
    prop: "imageinfo",
    iiprop: "url",
    titles: "File:Wikimedia Hackathon Prague 2019 - Group Photo - CLK - cropped.jpg"
};

api = api + "?origin=*";
Object.keys(params).forEach(function(key){api += "&" + key + "=" + params[key];});

fetch(api)
    .then(function(response){return response.json();})
    .then(function(response) {
        var pages = response.query.pages;
        for (var p in pages) {
            console.log(pages[p].title + " is at: " + pages[p].imageinfo[0].url);
        }
    })
    .catch(function(error){console.log(error);});

In MediaWiki environment:

var params = {
    action: 'query',
    format: 'json',
    prop: 'imageinfo',
    iiprop: 'url',
    titles: 'File:Wikimedia Hackathon Prague 2019 - Group Photo - CLK - cropped.jpg'
},
api = new mw.Api();

api.get( params ).done( function ( data ) {
    var pages = data.query.pages,
        p;
    for ( p in pages ) {
        console.log( pages[p].title + ' is at: ' + pages[p].imageinfo[0].url );
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文