首页 \ 问答 \ Ajax请求不是异步的(Ajax request are not async)

Ajax请求不是异步的(Ajax request are not async)

我有一个Ajax问题:

foreach(ids as id){
  $.ajax({
    url:'script.php',
    data:'id='+id,
    cache:false,
  });
}

如果我循环6次(在我的foreach循环中),我应该向服务器发出6个异步请求。 但在这种情况下,ajax调用是同步调用的,而不是异步的。 任何人都知道为什么会发生这种情况?


I have an ajax problem:

foreach(ids as id){
  $.ajax({
    url:'script.php',
    data:'id='+id,
    cache:false,
  });
}

If I loop 6 times (in my foreach loop) I should have 6 asynchronous requests being made to the server. But the ajax calls in this case are called synchronously, and not asynchronously. Any one have any idea why this happens?


原文:https://stackoverflow.com/questions/4900214
更新时间:2022-11-21 18:11

最满意答案

您可以使用UNION执行两个查询,其中一个查询直接在PNUM上匹配GROUP <> 1值,另一个匹配PNUM并通过group_members连接GROUP = 1值。

在第二部分中,您需要连接两次以使users获得与原始PNUM匹配的组成员。

由于GROUP条件各自相反,因此只有UNION一部分会返回结果。

/* First part of UNION directly matches PNUM for GROUP = 0 */
SELECT dloc
FROM users
WHERE 
  PNUM = 23101
  AND `group` <> 1
UNION
/**
 Second part of UNION matches GROUP = 1
 and joins through group_members back to users
 to get member dloc (from the second users join)
 */
SELECT uu.dloc
FROM users u
   INNER JOIN group_members m ON u.`GROUP` = 1 AND u.id = m.group_id
   INNER JOIN users uu ON m.member_id = uu.id
WHERE 
   u.PNUM = 23101

不幸的是,这需要在查询中放置两次PNUM值,每个UNION部分一次,但这并不是那么糟糕。

它在这里 (使用MySQL而不是SQLite,但这并不重要)

使用ORIN()子查询的原始方法,也可以完成,但我为GROUP = 0GROUP = 1添加了WHERE条件。

SELECT users.dloc 
FROM users 
WHERE users.id in (
    SELECT group_members.member_id 
    FROM group_members 
      INNER JOIN users on users.id = group_members.group_id 
    WHERE users.pnum='23101' AND `GROUP` = 1
 )
 OR users.id IN (
    SELECT users.ID
    FROM users 
    WHERE pnum='23101' AND `GROUP` = 0
);

这是替代方法......


You may use a UNION to execute both queries, one of which matches directly on PNUM for values where GROUP <> 1 and the other which matches on PNUM and joins through group_members for values where GROUP = 1.

In the second part, you need to join twice to users to get the members of the group matching the original PNUM.

Since the GROUP condition is opposite in each, only one part of the UNION will ever return results.

/* First part of UNION directly matches PNUM for GROUP = 0 */
SELECT dloc
FROM users
WHERE 
  PNUM = 23101
  AND `group` <> 1
UNION
/**
 Second part of UNION matches GROUP = 1
 and joins through group_members back to users
 to get member dloc (from the second users join)
 */
SELECT uu.dloc
FROM users u
   INNER JOIN group_members m ON u.`GROUP` = 1 AND u.id = m.group_id
   INNER JOIN users uu ON m.member_id = uu.id
WHERE 
   u.PNUM = 23101

This does unfortunately require placing the PNUM value twice in the query, once per UNION part, but that isn't so bad.

Here it is in action (using MySQL rather than SQLite, but that doesn't really matter)

Using your original method with OR and an IN() subquery, it can also be done, but I've added WHERE conditions for GROUP = 0 and GROUP = 1.

SELECT users.dloc 
FROM users 
WHERE users.id in (
    SELECT group_members.member_id 
    FROM group_members 
      INNER JOIN users on users.id = group_members.group_id 
    WHERE users.pnum='23101' AND `GROUP` = 1
 )
 OR users.id IN (
    SELECT users.ID
    FROM users 
    WHERE pnum='23101' AND `GROUP` = 0
);

And here's the alternative method in action...

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。