【发布时间】:2014-07-20 21:21:17
【问题描述】:
是否可以使用 jQuery 在另一个 getJSON 请求中使用 getJSON 请求?
类似这样的:
// Population the Requests List
// jQuery AJAX call for JSON
$.getJSON( '/workspace/friends/sentRequests', function( data ) {
// For each item in our JSON, add a table row and cells to the content string
$.each(data, function(){
//Loading each user data into global variable.
$.getJSON( '/workspace/friends/' + this.friendId, function( user ) {
requestListData.push(user);
});
requestTableContent += '<tr>';
requestTableContent += '<td><a href="#" class="linkshowuser btn btn-info btn-xs"" rel="' + this.friendId + '" title="Show Details">' + this.name + '</td>';
requestTableContent += '<td><a href="mailto:' + this.email + '">' + this.email + '</a></td>';
requestTableContent += '<td>' + moment(useListData[-1].activity.date_established).format('MMMM Do YYYY') + '</td>';
requestTableContent += '<td>' + this.gender + '</td>';
requestTableContent += '<td><a href="#" class="linkdeleteuser btn btn-danger btn-xs" rel="' + this.email + '">Danger!</a></td>';
});
// Inject the whole content string into our existing HTML table
$('#requestList table tbody').html(requestTableContent);
});
我想知道这是否可能?如果是这样,我在这里做错了什么......
编辑:我问这个是因为它应该使用来自 getJSON 请求的数据填充一个表,但事实并非如此。
【问题讨论】:
-
你怎么知道你做错了什么?像这样的异步请求没有“内部”;回调函数只是一个函数,它可以做任何其他函数可以做的任何事情。
-
解释没有按预期工作的地方
-
是的,这是可能的,但它显示了糟糕的设计:如果您正在调用 Web 服务来获取数据,那么设计它以便您只需在一个请求中获取所有内容。这里看起来每一行都有一个新请求:只获取整个表一次,而不是一次获取一行。
标签: javascript jquery ajax getjson