简体   繁体   English

$ .ajax jsonp回调函数的工作

[英]The working of $.ajax jsonp Callback function

I`m using http://www.myapifilms.com/imdb/inTheaters to fetch the data. 我正在使用http://www.myapifilms.com/imdb/inTheaters来获取数据。 This is the query Generated from the code below. 这是从下面的代码生成的查询

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

The issue here is I`m getting " parsererror; Error: jQuery1113009284638670545353_1442120413250 was not called ." 这里的问题是我收到“ parsererror;错误:未调用jQuery1113009284638670545353_1442120413250 ”。

I was just playing with passing values in parameters and could see that this works fine http://www.myapifilms.com/imdb?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix . 我只是在传递参数中的值,并且可以看到这很好用http://www.myapifilms.com/imdb?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix However this http://www.myapifilms.com/imdb/inTheaters?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix doesn`t. 但是,此http://www.myapifilms.com/imdb/inTheaters?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix没有

As you see from the latter query, it looks like the application is returning default callback "myapifilms". 从后面的查询中可以看到,该应用程序似乎正在返回默认回调“ myapifilms”。 So how do I use that default callback in the ajax request to access the data it has? 那么,如何在ajax请求中使用默认回调来访问其拥有的数据? Please help me in achieving the same. 请帮助我实现同样的目标。 Thanks in advance. 提前致谢。

Find my working here | 在这里找到我的工作| http://codepen.io/anon/pen/YywLop http://codepen.io/anon/pen/YywLop

I guess you can create a local function named myapifilms to handle the response. 我猜您可以创建一个名为myapifilms的本地函数来处理响应。

Something like this, and it should automatically get called. 这样的事情,它应该会自动被调用。

 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 + ", ");
            });
        }
    });
}

How said @Jaromanda X, it is a bug. 怎么说@Jaromanda X,这是一个错误。 In any case, the URL of your code is wrong, you have inTheat re s and is inTheat er s (mismatch between "re" and "er"). 无论如何,您的代码的URL是错误的,您拥有inTheat reinTheat er (“ re”和“ er”之间不匹配)。 I test this code and works: 我测试此代码并工作:

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 + ", ");
            });
        }
    });
}

PS: I'm the creator of site and I recommend you that migrate to version 2. PS:我是网站的创建者,我建议您迁移到版本2。

Edit: 编辑:

Example of call in Version 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);
}

In "json" variable you have all information. 在“ json”变量中,您具有所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM