【问题标题】:JSONP request with wikimedia使用 wikimedia 的 JSONP 请求
【发布时间】:2019-02-23 18:04:05
【问题描述】:

我正在使用 jsonp 向 wikimedia api 成功功能发出 ajax 请求,但错误处理不起作用。

var wikiRequestTimeout = setTimeout(function() {
        self.wikiData = "<p>No Articles Found</p>";
    }, 3000);

    var wikiUrl = 'https://en.wikipedia.org/w/api.php?action=opensearch&search= ' + this.title + '&format=json&callback=wikiCallback';
    $.ajax({
        url: wikiUrl,
        dataType: "jsonp",
        success: function(response) {
            var articleList = response[1];

            for (var i = 0; i < articleList.length; i++) {
                var articleStr = articleList[i];
                var url = 'http://en.wikipedia.org/wiki/' + articleStr;

                self.wikiData = '<li><a href="' + url + '">' + articleStr + '</a></li>';
            };
            clearTimeout(wikiRequestTimeout);

        }
    });
};

【问题讨论】:

  • 什么错误处理?
  • 如果请求失败或不返回任何数据,我想处理错误

标签: jquery ajax jsonp


【解决方案1】:

要对失败的 AJAX 请求进行错误处理,您需要指定错误回调函数。 http://api.jquery.com/jquery.ajax/

例如:

$.ajax({
    url: wikiUrl,
    dataType: "jsonp",
    success: function(response) {
        ...
    },
    error: function(jqXHR, textStatus, errorThrown) {
       // your error handling goes here
    }
});

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 2011-07-11
    • 2012-09-08
    • 2011-09-22
    • 2011-03-08
    • 2013-12-07
    • 2013-04-25
    • 2015-08-01
    相关资源
    最近更新 更多