Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么$.ajax函数第二次成功';这叫什么?_Javascript_Jquery_Ajax_Json_Rotten Tomatoes - Fatal编程技术网

Javascript 为什么$.ajax函数第二次成功';这叫什么?

Javascript 为什么$.ajax函数第二次成功';这叫什么?,javascript,jquery,ajax,json,rotten-tomatoes,Javascript,Jquery,Ajax,Json,Rotten Tomatoes,我正在试验烂番茄API。单击事件时,搜索将最匹配的电影标题和缩略图的JSON数据返回到搜索输入 完成第一个$.ajax请求后,使用var resultID发出第二个$.ajax请求,该请求在顶部声明,并在第一个$ajax请求“成功”时设置 到目前为止,当我第一次单击时,它成功地运行了第一个请求,但它无法运行return json.total(在“完成”时),因为resultID返回为“undefined”。但是第二次单击时,resultID显然已经设置好了,因为它将成功返回json.total

我正在试验烂番茄API。单击事件时,搜索将最匹配的电影标题和缩略图的JSON数据返回到搜索输入

完成第一个$.ajax请求后,使用var resultID发出第二个$.ajax请求,该请求在顶部声明,并在第一个$ajax请求“成功”时设置

到目前为止,当我第一次单击时,它成功地运行了第一个请求,但它无法运行return json.total(在“完成”时),因为resultID返回为“undefined”。但是第二次单击时,resultID显然已经设置好了,因为它将成功返回json.total

我猜这是一个与我如何声明和调用resultID(或异步触发ajax)有关的简单问题,但我不确定它是什么。有人能解释一下吗

var apiKey = '***********';
var resultID = "";

$('#search-submit').click(function(){
    var queryText = $('#movie-search').val();
    var queryURI = encodeURI(queryText);
    var searchApiRequest = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=" + queryURI + "&page_limit=10&page=1&apikey=" + apiKey;
    var reviewApiRequest = "http://api.rottentomatoes.com/api/public/v1.0/movies/" + resultID + "/reviews.json?review_type=top_critic&page_limit=10&page=1&country=us&apikey=" + apiKey;

    $.ajax({
        url: searchApiRequest,
        type: 'GET',
        dataType: 'jsonp',
        error: function() {
            console.log('there was error in processing the search request.');
        },
        success: function(json) {
            var movieInfo = json.movies[0];
            var noResultsMessage = 'your search returned no matches. Please try again.'; 
            var resultTitle = movieInfo.title;
            var resultThumb = movieInfo.posters.thumbnail;
            var resultDesc = movieInfo.critics_consensus;

            var reviewsPublicURL = movieInfo.links.reviews;
            var reviewsRequest = reviewsPublicURL;

            resultID = movieInfo.id; // supposed to set global var resultID to movieID

            $('.search-results').find('img').attr('src',resultThumb).end().find('h1').text(resultTitle).end().find('p').text(resultDesc);

        },
        complete: function() {
            $.ajax({
                url:reviewApiRequest,
                type: 'GET',
                dataType: 'jsonp',
                error: function(json) {
                    console.log('for whatever reason, we could not find any reviews of this movie');
                },
                success: function(json) {
                    console.log(json.total);
                } 
            });
        }
    });
});

您不仅需要设置全局
resultID
,还需要刷新
reviewPairRequest
。当你更新
resultID

时,它本身并不会神奇地改变它的值,因为它是一个旁注
(函数($){…
不是一个DOM就绪的句柄。我将ReviewPaireRequest的声明移到了成功函数中,它成功了。谢谢你的提示!