te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Can I make two ajax request in one hit in JavaScript or jQuery? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I make two ajax request in one hit in JavaScript or jQuery? - Stack Overflow

programmeradmin3浏览0评论

Can I make two or multiple Ajax requests in one hit in JavaScript or jQuery?

I mean I know it seems crazy to ask this question, but earlier I have been through an interview and they asked me this question. After the interview I searched a lot on this but found nothing.

Somewhere I just found that you can put another Ajax request as the callback of first one. But this is not the real story at all.

I have a doubt, does sync or async has some role in this?

If somebody has a solution, a POC on jsfiddle or plunkr will be appreciated on the same.

JavaScript experts, please help. Thanks in Advance!!

Can I make two or multiple Ajax requests in one hit in JavaScript or jQuery?

I mean I know it seems crazy to ask this question, but earlier I have been through an interview and they asked me this question. After the interview I searched a lot on this but found nothing.

Somewhere I just found that you can put another Ajax request as the callback of first one. But this is not the real story at all.

I have a doubt, does sync or async has some role in this?

If somebody has a solution, a POC on jsfiddle or plunkr will be appreciated on the same.

JavaScript experts, please help. Thanks in Advance!!

Share Improve this question edited Sep 16, 2014 at 14:08 Ashish Kumar asked Jan 3, 2014 at 15:36 Ashish KumarAshish Kumar 3,0393 gold badges19 silver badges27 bronze badges 7
  • Is this what you're looking for? stackoverflow./questions/561046/… – elclanrs Commented Jan 3, 2014 at 15:38
  • 5 Just curious, what did you answer at your interview? – Maen Commented Jan 3, 2014 at 15:39
  • 1 I naswered them NO. It's not possible, but they told me - "Its very much possible" :( – Ashish Kumar Commented Jan 3, 2014 at 15:43
  • How can you be so sure "one hit" means one usage of $.ajax? if the correct answer was "yes", then clearly that isn't what they meant because $.ajax only performs one ajax request. In that case i would have asked for clarification before answering. Knowing the right questions to ask is just as important as knowing the answers. – Kevin B Commented Jan 3, 2014 at 15:56
  • They didn't answer me how, that's why I am here with the same question... – Ashish Kumar Commented Jan 3, 2014 at 15:58
 |  Show 2 more ments

6 Answers 6

Reset to default 4

If you are using jQuery you can make use of the deferred objects. Basically you can perform multiple ajax requests, and when all are done, one callback is executed.

Have a look at http://api.jquery./jquery.when/ for more information. There's also a simple example:

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )   
    .then( myFunc, myFailure );

In short -- no, you cannot.

You can make multiple callbacks to do them all in order, or you can make all the requests to the multiple points on the server side.

You can always do two ajax requests in a row, but there is no guarantee in what order they will return to their respective callbacks.

Look, if you want to send 2 urls on single xmlHttpRequest, I think, this is not possible.

And suppose, it were possible, how we would be able to find the data send by server as response that which response is for which url request?

I'm not sure what you mean by hit? but yes put plainly.

async - doesn't wait for the response from the ajax request

sync - waits until a response from the ajax

depends on what you want to do.

EDIT: after reading other responses. to clarify, if you want multiple concurrent simultaneous ajax requests you need a concurrent connection for each. that is how it possible.

Call 2 requests on same event, make them async.

$("#btn").click(function(){$.get("foo.php");$.get("bar.php");}); 

you can do only two ajax hits with when and then, but if you wants more than two ajax hits then I would remend ajax always.

    $.ajax({
           url: '/Job/multipleajax/',
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           type: "POST",
           data: JSON.stringify(id),
           success: function (response) {
    alert("first ajax hit");
    }
    }).always(function () {
    // second ajax call here
    }).always(function () {
    // Third ajax call code  here
    })
发布评论

评论列表(0)

  1. 暂无评论