JSONP回调错误:对象[object Object]的属性“insertFilmData”不是函数

时间:2013-06-23 15:26:50

标签: javascript jquery ajax json jsonp

我使用JSONP进行了3次嵌套$ .ajax调用,第一次$ .ajax调用在函数中工作,但第三次无法执行回调给我这个错误:Uncaught TypeError: Property 'insertFilmData' of object [object Object] is not a function

CODE:

function searchFilms(film, settings) {
if (film === '') {
    console.log("enter a movie");
} else {
    $.ajax({
        type: 'GET',
        url: 'http://api.themoviedb.org/3/configuration?api_key=XXX',
        async: false,
        jsonpCallback: 'getSearchResults',
        contentType: 'application/json',
        dataType: 'jsonp',
        success: function(json) {
            if (json.images) {
                settings.imageData = json.images;
                getSearchResults(json, settings);
            }
        }
    });

    function getSearchResults(data, settings) {
        $.ajax({
            type: 'GET',
            url: 'http://api.themoviedb.org/3/search/movie?api_key=XXX&query=' + film,
            async: false,
            jsonpCallback: 'deliverResults',
            contentType: 'application/json',
            dataType: 'jsonp',
            success: function(json) {
                if (json.total_results !== 0) {
                    deliverResults(json, settings);
                }
            }
        });

        function deliverResults(data, settings) {
            var results = data.results;
            var html = "<ul id='film-search-results' data-role='listview' data-inset='true'>";
            for (var i = 0; i < results.length; i++) {
                var result = results[i];
                var filmId = result.id;
                $.ajax({
                    type: 'GET',
                    url: 'http://api.themoviedb.org/3/movie/' + filmId + '?api_key=XXX&append_to_response=' + settings.extras,
                    async: false,
                    jsonpCallback: 'insertFilmData',
                    contentType: 'application/json',
                    dataType: 'jsonp',
                    success: function(json) {
                        if (json) {
                            settings.html = html;
                            insertFilmData(json, settings);
                        }
                    }
                });

                function insertFilmData(data, settings) {
                    var poster_base_url = settings.imageData.base_url;
                    var poster_size = settings.imageData.poster_sizes[0];
                    var poster_filepath = data.poster_path;
                    var poster_url = poster_base_url + poster_size + "/" + poster_filepath;
                    var html = settings.html + "<li><img src='" + poster_url + "' alt='Poster' /><a  class='ui-link-inherit'><h3 class='ui-li-heading'>" + data.title + "</h3><p class='ui-li-desc'><strong>" + data.tagline + "</strong></p><p class='ui-li-desc'>" + data.overview + "</p></a></li>";

                }
            }
            html = html + "</ul>";
            console.log(html);
            $('#content-add-title').html(html);
            $('#film-search-results').trigger("create");
        }
    }
}
}

我很擅长使用JSONP进行跨域调用,所以我不确定看到前两个回叫是什么时会出现问题,而且只有最后一个没有回复。任何帮助或方向将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

它正在寻找一种名为insertFilmData的方法,而无法找到它。据推测,如果将函数定义为作为ajax调用的参数的对象的方法,它将起作用,即:

            $.ajax({
                type: 'GET',
                url: 'http://api.themoviedb.org/3/movie/' + filmId + '?api_key=XXX&append_to_response=' + settings.extras,
                async: false,
                jsonpCallback: 'insertFilmData',
                contentType: 'application/json',
                dataType: 'jsonp',
                success: function(json) {
                    if (json) {
                        settings.html = html;
                        insertFilmData(json, settings);
                    }
                },
                insertFilmData: function(data, settings) {
                    var poster_base_url = settings.imageData.base_url;
                    var poster_size = settings.imageData.poster_sizes[0];
                    var poster_filepath = data.poster_path;
                    var poster_url = poster_base_url + poster_size + "/" + poster_filepath;
                    var html = settings.html + "<li><img src='" + poster_url + "' alt='Poster' /><a  class='ui-link-inherit'><h3 class='ui-li-heading'>" + data.title + "</h3><p class='ui-li-desc'><strong>" + data.tagline + "</strong></p><p class='ui-li-desc'>" + data.overview + "</p></a></li>";

                }

            });

或者,您可以保持现状,但将行jsonpCallback: 'insertFilmData',更改为jsonpCallback: insertFilmData,。这将意味着ajax调用的一个参数将是函数本身,而不是函数的名称。 (如果我理解正确,那么函数定义低于这一行并不重要,因为它是一个命名函数,它在范围内。)