gpt4 book ai didi

javascript - 为什么 jQuery 不使用 $.when 遍历两个函数中的所有 JSON 项

转载 作者:行者123 更新时间:2023-12-03 12:31:40 27 4
gpt4 key购买 nike

当我使用 Chrome 网络检查器检查时,从两个 ajax 函数返回的 json 似乎已下载完成。然而,jQuery each 并没有遍历两个 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"
}]

最佳答案

看看 documentation 中的 $.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!" );
}
});

正如所解释的,传递给回调函数的参数是[ data, statusText, jqXHR ] 形式的数组。数组的第一个元素包含实际响应。所以你必须将你的函数更改为

function myExecute(movieCollectionData, movieFavoritesData)
{
buildList(movieCollectionData[0], movieCollectionElement);
buildList(movieFavoritesData[0], movieFavoritesElement);
}
<小时/>

单一案例之所以有效,是因为 $.when 没有创建新的 Promise,它只是返回 $.ajax 返回的 Promise,因此它不会聚合多重 promise 的结果。

关于javascript - 为什么 jQuery 不使用 $.when 遍历两个函数中的所有 JSON 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23900605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com