Ajax 查询 sql 并发送单行响应
我认为我正在做一个经典的 ajax 脚本,对于使用过它的人来说应该相当容易。
好吧,这个想法是从 jquery ajax 调用一个页面,该页面计算并循环一些数据。我希望页面在计算出 1 行时响应 JavaScript。然后 jquery 应该为 row 填充一个 div 行。这意味着它不应等待所有行完成,而应一次只获取一行。
有道理吗?这个问题应该是众所周知的,但很抱歉我的解释不好。到目前为止我的代码:
Jquery 页面:
$(document).ready(function() {
loadCityFriends();
});
function loadCityFriends() {
$.ajax({
url: 'cityfriends.php',
success: function(data) {
$("#cityfriends").append(data);
}
});
}
和 cityfriends.php (计算页面):
foreach bla bla bla
print '{"fbid": "'.$friend["id"].'", "img": "'.$friend["picture"].'"}';
我的代码等到 cityfriends.php 完成循环,然后填充 div。
I think I'm doing a classical ajax script which should be fairly easy for the people who have worked with it.
Alright the idea is to call a page from jquery ajax which calculates and loops through some data. I want the page to respond back to the javascript whenever it got 1 row calculated. The jquery should then populate a div row for row. Meaning it should NOT wait for all rows to complete, but just take one row at a time.
Does it make sense? The problem should be well-known, but sorry for my bad explanation. My code so far:
Jquery page:
$(document).ready(function() {
loadCityFriends();
});
function loadCityFriends() {
$.ajax({
url: 'cityfriends.php',
success: function(data) {
$("#cityfriends").append(data);
}
});
}
And cityfriends.php (the page calculating):
foreach bla bla bla
print '{"fbid": "'.$friend["id"].'", "img": "'.$friend["picture"].'"}';
My code wait until cityfriends.php are done looping and then populates the div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行此操作的唯一方法是对 PHP 脚本进行多次独立调用,可能首先请求行数,然后按顺序请求每一行。 PHP 在完成执行之前不会返回,因此通过一次 ajax 调用,您将查看计算后返回的所有行或仅查看第一行。
像这样的事情会起作用:
PHP
然后使用 ajax 首先轮询 getRows() ,然后轮询 returnRow getRows() 返回的适当行数;
显然,您需要清理输入并优化代码,但这应该是总体思路。
The only way to do this would be to make multiple independent calls to the PHP script probably first requesting the number of rows then afterwards requesting each row sequentially. PHP won't return before it's finished executing so with one ajax call you'd be looking at either all of the rows returned after calculation or just the first one.
Something like this would work:
PHP
Then use ajax to first poll getRows() and then poll returnRow the appropriate number of rows returned by getRows();
Obviously you'll need to sanitize inputs and optimize the code but that should be the general idea.