0

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

代码:

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 进行跨域调用非常陌生,所以我不确定看到前两个回调触发时出了什么问题,而只有最后一个没有触发。任何帮助或方向将不胜感激。谢谢

4

1 回答 1

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 调用的参数之一将是函数本身,而不是函数的名称。(如果我理解正确,函数定义在这一行下方并不重要,因为它是一个命名函数并且它在范围内。)

于 2013-06-23T17:14:27.130 回答