4

我正在编写一个脚本,我需要循环一组 AJAX 请求:

$('#fetchPosts').click(function(){ 

    for(var i=0; i < link_array.length; i++) {

        settings = {
           // some object not relevant
        }

        var status = main_ajaxCall(settings, i); // ajax call
     }
});

function main_ajaxCall(settings, i) {

   $.ajax({ 
     type: "POST",
     url: "../model/insert.php",
     data:{obj_settings: settings},
     dataType: "json",
     cache: false,
     success: function (data) {
         // some handeling here
         return 0;
     },
     error: function(XMLHttpRequest, textStatus, errorThrown) {
         return 1;
     },
};

为什么 AJAX 请求会立即触发?它似乎没有等待来自 model/insert.php 的响应,有没有办法强制它在触发下一个 AJAX 请求之前等待响应?

编辑1:

我好像不太清楚,对不起,我不想让它等待,我想排队。

我无法在一个请求中拨打电话,这在我目前的情况下是不可能的。

4

3 回答 3

4

Set async to false if you want to wait for a response (default: true)

$.ajax({
    async: false,
    ...

http://api.jquery.com/jQuery.ajax/

If you do not want blocking, you can set a success handler function using .ajaxComplete(), and you have to keep track of active AJAX connections if you want to wait for all to complete - How to know when all ajax calls are complete

The best solution would be to minimize the number of AJAX requests to one. If you have to make a loop of AJAX requests, the logic could be simplified somewhere (put that in the server perhaps?)

EDIT 1: (In response to OP edit)

If you want to queue the AJAX requests, this question has been answered before here:

You could also use these libraries (all you needed to do was Google):

于 2013-07-08T02:47:28.143 回答
1

It fires instantly and doesn't wait around because that's what AJAX does best (The first A stands for asynchronous).

The request to a server could take a long time to respond, and in most cases, don't want user's browser's freezing up or stopping them from doing anything else. If you do, you could probably just use a normal request.

This is the reason you give it functions for success error, so it can call them when the server responds.

If you want nothing to be able to happen in the browser while you're calling insert.php, you could drop an overlay (eg. dark div) over everything with a loading image and remove it on success.

Maybe replace the $('#fetchPosts') element with "loading..." text and then reverse it when done. Hiding visibility of the fetchPosts element and adding a different "loading.." element is a nice way.

于 2013-07-08T02:49:16.820 回答
0

Your AJAX call will wait for a response from the server, but wil do so asynchronously. That is, your script will continue to execute rather than block the browser while the server responds. When the server responds (or when the request times out - usually several seconds) your success: or error: functions will then execute.

The effect of your code here is to create several concurrent requests based on the link_array length.

You could specify async:false in your AJAX call, but this would freeze the browser while all the AJAX calls are made.

You should rewrite your code to execute all the handling as part of your success: function. I'd recommend you rewrite your code to assemble all your request into one, and make one AJAX call rather than several, and have the server return all the responses as one block. I can't suggest exactly how you do that - it's implementation dependent.

EDITED: In response to your clarification, if you want them to be called in order, you'll need the success function to call the next one. You'll then have a chain of success calls the next, whose success calls the next, whose success calls the next.. etc until the last one which does the final processing. One way would be to pass the call number to the success function.

于 2013-07-08T02:48:27.600 回答