℡Wang Yan 2018-09-19 14:15 采纳率: 100%
浏览 24

Wikimedia的JSONP请求

I am making ajax request using jsonp to the wikimedia api success function works but the error handling is not working.

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);

        }
    });
};
  • 写回答

1条回答 默认 最新

  • weixin_33674976 2018-09-19 14:32
    关注

    To get error handling for failing AJAX requests you need to specify an error callback function. http://api.jquery.com/jquery.ajax/

    E.g:

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

报告相同问题?