weixin_33722405 2016-04-04 15:54 采纳率: 0%
浏览 29

维基百科AJAX通话

I have this code, it does an AJAX call to Wikipedia, asking for results of a given query (var searchText):

function main() {
    $(".btn").click(function() {
        $("#iframe").attr('src', 'https://en.wikipedia.org/wiki/Special:Random');
        $(".embed-container").css('visibility', 'visible');
    });

    function wikiAjax (searchURL) {
        return $.ajax({
            url: searchURL,
            jsonp: "callback",
            dataType: 'jsonp',
            xhrFields: {
                withCredentials: true
            }
        });
    }

    $(".search-form").submit(function() {
        event.preventDefault(); 
        var searchText = $('#search').val();
        var searchURL = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrsearch=" + searchText + "&gsrlimit=15&prop=extracts&exsentences=3&exintro=&explaintext&exlimit=max&callback=JSON_CALLBACK";
        console.log(searchURL);
        var wikiResponse = wikiAjax(searchURL);
        wikiResponse.done(function(data) {
            console.log(data);
        }).fail(function(err) {
            alert("The call has been rejected");
        });
    });
}

$(document).ready(main);

But it returns me a strange object:

image

Could someone please help me?

  • 写回答

3条回答 默认 最新

  • csdn产品小助手 2016-04-04 16:03
    关注

    The Ajax call needs to have 3 parameters. There is JSON data in the 3rd param. Try it with this jsfiddle.

    wikiResponse.done(function(error, success, data) {
        console.log(data.responseJSON.query.pages);
    })
    
    评论

报告相同问题?