【问题标题】:The working of $.ajax jsonp Callback function$.ajax jsonp回调函数的作用
【发布时间】:2015-09-15 13:57:05
【问题描述】:

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

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

【问题讨论】:

  • jsonpCallback: 'myapifilms', 添加到 ajax 调用 - 编辑:嗯,似乎不起作用
  • 是的……没错……也试过了……
  • 可能是该网站因该请求而损坏 - 您是否尝试联系网站所有者?

标签: javascript jquery json ajax callback


【解决方案1】:

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

类似这样的东西,它应该会自动被调用。

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

【讨论】:

    【解决方案2】:

    @Jaromanda X 怎么说,这是一个错误。无论如何,您的代码的 URL 是错误的,您有 inTheatres 并且是 inTheaters (“re”和“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 + ", ");
                });
            }
        });
    }
    

    P.S.:我是网站的创建者,我建议您迁移到版本 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”变量中,您拥有所有信息。

    【讨论】:

    • 嗨 Alberto,这在 version2 中会是什么样子
    猜你喜欢
    • 2011-11-28
    • 2014-08-14
    • 2011-11-15
    • 2019-12-07
    • 2012-09-12
    • 1970-01-01
    • 2011-09-27
    • 2013-04-25
    相关资源
    最近更新 更多