$的工作.ajax json回调函数

The working of $.ajax jsonp Callback function

本文关键字:回调 函数 json ajax 工作      更新时间:2023-09-26

我使用http://www.myapifilms.com/imdb/inTheaters来获取数据。这是由下面的代码生成的查询。

function sendRequest() {
    var parms = "format=JSONP";
    // Other parameters
    //parms += "&lang=en-us&actors=S";
    $("#countries").text("");
    $("#actors").text("");
    $.ajax({
        data:       parms,
        url:        'http://www.myapifilms.com/imdb/inTheatres',
        type:       'get',
        dataType:   'jsonp',
        beforeSend: function () {alert(this.url);},

        success:  function (response, textStatus, jqXHR) {
            $.each(response, function(index, element){
                if (element.directors != undefined) {
                    $.each(element.directors, function(index, director){
                        $("#directors").append(director.name + ", ");
                    });
                }
                if (element.title != undefined) {
                    $.each(element.title, function(index, title){
                        $("#movies").append(title + ", ");
                    });
                }
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            $("#error").text(textStatus + "; " + errorThrown);
        }
    });
}

这里的问题是我得到"parsererror;错误:jQuery1113009284638670545353_1442120413250未被调用。"

我只是在参数中传递值,可以看到这工作得很好http://www.myapifilms.com/imdb?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix。但是这个http://www.myapifilms.com/imdb/inTheaters?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix没有。

正如你从后面的查询中看到的,看起来应用程序正在返回默认回调"myapifilms"。那么,如何在ajax请求中使用默认回调来访问它拥有的数据呢?请帮助我实现同样的目标。提前感谢。

在这里找到我的作品| http://codepen.io/anon/pen/YywLop

我想你可以创建一个名为myapfilms的本地函数来处理响应。

就像这样,它应该被自动调用。

 function myapifilms(response) {
     $.each(response, function(index, element){
        if (element.directors != undefined) {
            $.each(element.directors, function(index, director){
                $("#directors").append(director.name + ", ");
            });
        }
        if (element.title != undefined) {
            $.each(element.title, function(index, title){
                $("#movies").append(title + ", ");
            });
        }
    });
}

@Jaromanda X怎么说,这是一个bug。在任何情况下,你的代码的URL是错误的,你有inTheatresinTheaters("re"answers"er"之间不匹配)。我测试了这段代码并运行:

function submit() {
    var url = "http://www.myapifilms.com/imdb/inTheaters";
    $.ajax({
       data:      'format=JSONP',
       url:       url,
       dataType:  'jsonp',
       jsonpCallback: "myapifilms",
       success:   function (response, textStatus, jqXHR) {
            $.each(response, function(index, element){
                if (element.directors != undefined) {
                    $.each(element.directors, function(index, director){
                        $("#results").append(director.name + ", ");
                    });
                }
            });
       },
        error: function(jqXHR, textStatus, errorThrown) {
            $("#error").text(textStatus + "; " + errorThrown);
        }
    });
}
function myapifilms(response) {
     $.each(response, function(index, element){
        if (element.directors != undefined) {
            $.each(element.directors, function(index, director){
                $("#results").append(director.name + ", ");
            });
        }
    });
}

注::我是网站的创建者,我建议你迁移到版本2。

编辑:

版本2中的调用示例:

function submit() {
    var url = "http://www.myapifilms.com/imdb/inTheaters";
    var params = {
        token: 'YOUR_TOKEN',
        format: 'json',
        callback: 'myapifilms'
    };
    $.ajax({
        url : url,
        data : params,
        type : 'get',
        dataType : 'jsonp',
        jsonpCallback: 'myapifilms'
    });
}
function myapifilms(json) {
    alert(json.data.inTheaters[0].openingThisWeek);
}

在"json"变量中包含所有信息