Ajax multiple requests at once

I recently developed a project when I sent multiple ajax requests on one aspx page. I also set a timer, this request occurs with an interval of 5 seconds. Everything seems to be working fine until the reaction starts. I know that I could do the same with one request, but I wonder why this is happening. I looked around the Internet, but have not yet found a solution. I now actually adopted this coding style of how to make a single request with multiple results, but I'm curious, and I really want to know how to make multiple ajax requests, as the answer will not be mixed. This is my sample code:

 $(document).ready(function () {
        var a = setInterval("request1()", 5000);
        var b = setInterval("request2()", 5000);
    });

function request1() {

        $.ajax({
            url: 'ajaxhandler.aspx?method=method1' ,
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=utf-8');
            },
            success: function (data) {
                  alert(data);
            }

        });
    }


function request2() {

        $.ajax({
            url: 'ajaxhandler.aspx?method=method2' ,
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=utf-8');
            },
            success: function (data) {
                  alert(data);
            }

        });
    }
+5
source share
4 answers

, , AJAX ( "a" "" ).

, request2() "" .

function request1() {
    $.ajax({
        url: 'ajaxhandler.aspx?method=method1' ,
        beforeSend: function (xhr) {
            xhr.overrideMimeType('text/plain; charset=utf-8');
        },
        success: function (data) {
              request2();
        }

    });
}
+4

JavaScript, , . AJAX, . aspx methods, .

. aspx ? , ( ) , , . HTTP .

function requestAll() {

        $.ajax({
            url: 'ajaxhandler.aspx' ,
            data: ['method1', 'method2'],
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=utf-8');
            },
            success: function (data) {
                  alert(data);
            }

        });
    }
+2

URL- JSON . JSON.

, , , . DOM node .

function multi_getJSON(urls, callback)
{
    // number of ajax requests to wait for
    var count = urls.length;

    // results of individual requests get stuffed here
    var results = [];

    // handle empty urls case
    if(count === 0)
    {
        callback(results);
        return;
    }

    urls.forEach(function(url, index)
    {
        $.getJSON(url, function(data)
        {
            results[index] = data;
            count = count - 1;

            // are we finished?
            if(count === 0)
            {
                callback(results);
            }
        });
    });
}
+2

, . , , , , .

, , - (, onkeyup). - , , , - . "", , "tes" "te". , , , , .

Encoding a timestamp (or a unique identifier, counter, version number, key, etc.) will allow you to check this to make sure you have the most up-to-date answer or to sort the results by timestamp if you need to guarantee an order, etc. .d.

+1
source

All Articles