为什么jQuery不使用$ .when通过两个函数遍历所有JSON项

乔尔·Z。

当我使用Chrome Network检查器检查时,从两个ajax函数返回的json似乎已下载完成。但是,jQuery并没有遍历两个json文件的所有项目。它仅遍历json文件的前三项。因此,每个jQuery只构建前三部电影的电影标题。

如果我仅使用$ .when和一个函数,它将工作并返回所有项目。我注释了可以作为一个单一功能使用的两个领域。

jQuery 1.11.0

这是JavaScript:

$(document).ready(function()
{
    var movieCollectionUrl = 'json/movie_collection.json';
    var movieFavoritesUrl = 'json/movie_favorites.json';
    var movieCollectionElement = $('#collection ul');
    var movieFavoritesElement = $('#favorites ul');

    function getList(url)
    {
        return $.ajax(
        {
            dataType: "json",
            type: "get",
            url: url
        });
    }

    function buildList(data, element)
    {
        $.each(data, function(i)
        {
            var title = data[0][i].title; 
            //var title = data[i].title; //this returns all items but only with a single function with $.when
            var html = '<li>' + title + '</li>';
            element.append(html);
        });
    }


    function myExecute(movieCollectionData, movieFavoritesData)
    {
        buildList(movieCollectionData, movieCollectionElement);
        buildList(movieFavoritesData, movieFavoritesElement);
    }

    /*
    function executeSingle(movieFavoritesData)
    {
        buildList(movieFavoritesData, movieFavoritesElement);
    }
    */

    function myFail()
    {
        alert("FAILED TO LOAD");
    }

    $.when(getList(movieCollectionUrl), getList(movieFavoritesUrl)).then(myExecute, myFail);
    //$.when(getList(movieFavoritesUrl)).then(executeSingle, myFail); //This one works. Using only one function here
}); //document ready

index.html

<!DOCTYPE HTML>
    <head>
        <title>Movies</title>
    </head>
    <body>
        <div id="favorites">
            <h2>Favorite Movies</h2>
            <ul></ul>
        </div>
        <div id="collection">
            <h2>Movie Collection</h2>
            <ul></ul>
        </div>
    </body>
</html>

movie_collection.json

[
{
    "title": "127 Hours",
    "poster": "http://slurm.trakt.us/images/posters_movies/6646.1.jpg"
},
{
    "title": "2012",
    "poster": "http://slurm.trakt.us/images/posters_movies/463.jpg"
},
{
    "title": "The 40 Year Old Virgin",
    "poster": "http://slurm.trakt.us/images/posters_movies/658.1.jpg"
},
{
    "title": "A Better Life",
    "poster": "http://slurm.trakt.us/images/posters_movies/182444.1.jpg"
},
{
    "title": "A Moment To Remember",
    "poster": "http://slurm.trakt.us/images/posters_movies/162086.jpg"
}]

movie_favorites.json

[
{
    "title": "A Seperation",
    "poster": "http://slurm.trakt.us/images/posters_movies/176861.2.jpg"
},
{
    "title": "Abraham Lincoln Vampire Hunter",
    "poster": "http://slurm.trakt.us/images/posters_movies/184657.2.jpg"
},
{
    "title": "The Adventures of Tin Tin",
    "poster": "http://slurm.trakt.us/images/posters_movies/13066.2.jpg"
},
{
    "title": "Agore",
    "poster": "http://slurm.trakt.us/images/posters_movies/223.jpg"
}]
费利克斯·克林(Felix Kling)

看一看的例子$.when的文件

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
  // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
  // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
  var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
  if ( /Whip It/.test( data ) ) {
    alert( "We got what we came for!" );
  }
});

如前所述,传递给回调函数的参数是form的数组[ data, statusText, jqXHR ]数组的第一个元素包含实际响应。因此,您必须将功能更改为

function myExecute(movieCollectionData, movieFavoritesData)
{
    buildList(movieCollectionData[0], movieCollectionElement);
    buildList(movieFavoritesData[0], movieFavoritesElement);
}

单个案例之所以有效,是因为$.when它不会创建新的承诺,它只会返回要返回的承诺$.ajax,因此不会汇总多个承诺的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么 onchange 函数不使用 Jquery 调用 DropDownList?

为什么OpenGL函数不使用模板?

为什么不使用回调函数?

为什么“ fopen”函数不使用枚举?

为什么stream parallel()不使用所有可用线程?

为什么不使用所有进口商品?

为什么GPG不使用所有子密钥解密?

一个CORS POST请求可以在普通JavaScript中工作,但是为什么不使用jQuery?

为什么堆栈不使用定义的额外依赖项?

为什么哈希函数不使用随机性?

为什么函数printk()不使用逗号分隔参数?

scrapy:为什么不使用parse_item函数

/ etc / fstab为什么不使用XML或JSON?

为什么不使用相同的值更新两个 Double 变量?

为什么不使用没有动作修饰符的apt-get install --qq为什么不使用?

为什么JavaScript Promises有时既不使用.catch也不使用.then?

在两个矩阵的所有行对上使用函数

为什么命令行现在每个参数后面都有两个连字符(-)?它起源于何处,为什么不使用单个连字符?

为什么我的 GPU 在使用共享内存之前不使用所有专用内存?

当我使用函数 date( ) 中包含的 datetime 类型列而不使用 date( ) 时,为什么会得到两个不同的记录计数?

在比较两个可为空的变量时,为什么不使用短路逻辑“和”运算符?

为什么不使用 jQuery 呈现 css?

为什么 Jquery 不使用 textarea?

为什么jQuery有两个版本?

为什么不使用WebSphere Liberty Tools for Eclipse发布所有资源?

为什么IIS或Apache在满载时不使用所有CPU容量?

为什么Spark Standalone群集不使用所有可用核心?

clojure pmap-为什么我不使用所有内核?

为什么Spark(在Google Dataproc上)不使用所有vcore?