Asynchronous data, return the results disorder problem

Ajax.js
function SendHttp(url, data, callback, context, noRoot) {
    return ajax_request(url, data, callback, context, noRoot);
}

var requests = new Array(); //alert(requests.length);

if (typeof(XMLHttpRequest) == 'undefined')
    var XMLHttpRequest = function() {
        var request = null;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
            request.setTimeouts(20000, 20000, 50000, 100000);
        } catch(e) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            } catch(ee) {
            }
        }
        return request;
    };

function ajax_stop() {
    for (var i = 0; i <requests.length; i++) {
        if (requests[i] != null) {
            requests[i].obj.abort();
          
        }
    }
}

function ajax_create_request(context) {
    for (var i = 0; i <requests.length; i++) {
        if (requests[i].readyState == 4) {
            requests[i].abort();
            requests[i].context = null;
            return requests[i];
        }
    }

    var pos = requests.length;

    requests[pos] = Object();
    requests[pos].obj = new XMLHttpRequest();
    requests[pos].context = context;

    return requests[pos];
}

//Send a AJAX request, the SQL statement execution result
function ajax_request(url, data, callback, context, noRoot) {
    var request = ajax_create_request(context);
    var async = typeof (callback) == 'function';

    if (async)
        request.obj.onreadystatechange = function() {
            if (request.obj.readyState == 4)
                callback(new ajax_response(request));
            //Asynchronous object as a parameter of the callback function, has three attributes: value,context,error	
        };

    request.obj.open('POST', url, async);
    
    request.obj.send(data);
  
    if (!async) {
        //If synchronization is a direct return value,
        var o = new ajax_response(request);
        return o.value;
    }
};

function ajax_response(request) {
    this.request = request.obj;
    this.error = null;
    this.value = null;
    this.context = request.context;

    if (request.obj.status == 200) {
        try {
            this.value = object_from_json(request);

            if (this.value && this.value.error) {
                this.error = this.value.error;
                this.value = null;
            }
        }
        catch (e) {
            this.error = new ajax_error(e.name, e.description, e.number);
        }
    }
    else {
        this.error = new ajax_error('HTTP request failed with status: ' + request.obj.status, request.obj.status);
    }

    return this;
}

function enc(s) {
    return s.toString().replace(/\%/g, "%26").replace(/=/g, "%3D");
}

function object_from_json(request) {
    if (request.obj.responseXML != null && request.obj.responseXML.xml != null && request.obj.responseXML.xml != '')
        return request.obj.responseXML;

    //var r = null;	
    //eval('r=' + request.obj.responseText + ';');
    //return r;
    return request.obj.responseText;
}

function ajax_error(name, description, number) {
    this.name = name;
    this.description = description;
    this.number = number;

    return this;
}

ajax_error.prototype.toString = function() {
    return this.name + " " + this.description;
};



Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="../lib/js/Ajax.js" type="text/javascript"></script>
    <script src="../lib/jquery/jquery-1.3.2.min.js" type="text/javascript"> </script>
    <script type="text/javascript" language="javascript">
        $(pageLoad);
        var html = "";
        function pageLoad() {

            SendHttp("../Handler/select.ashx?t=" + Math.random() + "&view=AssetType&idField=AssetTypeID&textField=AssetTypeName&where=AssetTypeID=1001",
                    "",
                    function (data) {
                        html += " 1:  " + data.value + " \n\n";
                        $("#divTest").html(html);
                    }, null, '');
            SendHttp("../Handler/select.ashx?t=" + Math.random() + "&view=AssetType&idField=AssetTypeID&textField=AssetTypeName&where=AssetTypeID=1002",
                    "",
                    function (data) {
                        html += " 2:  " + data.value + " \n\n";
                        $("#divTest").html(html);
                    }, null, '');
            SendHttp("../Handler/select.ashx?t=" + Math.random() + "&view=AssetType&idField=AssetTypeID&textField=AssetTypeName&where=AssetTypeID=1003",
                    "",
                    function (data) {
                        html += " 3:  " + data.value + " \n\n";
                        $("#divTest").html(html);
                    }, null, '');
        }
    </script>
</head>
<body>
    <div id="divTest">
    </div>
</body>
</html>


Return result:
1: 2: [{"id":1002,"value":1002,"text":"Tables and chairs "}] 3: [{" Id ": 1003," value ": 1003," text ":" daily equipment"}]

Refresh:
1: [{"id":1001,"value":1001,"text":"The computer helo "}] 2: [{" Id ": 1001," value ": 1001," text ":" helo "}] 3: [{" Id ": 1003," value ": 1003," text ":" daily equipment"}]

The above results for an operation
==========================================================
The results obtained have:
1: [{"id":1001,"value":1001,"text":"The computer helo "}] 2: [{" Id ": 1002," value ": 1002," text ":" 3: "and}] [{" Id ": 1003," value ": 1003," text ":" daily equipment"}]

The use of asynchronous data acquisition, data is returned will disorder, I would like to ask how to correctly use asynchronous programming, in order to ensure the accuracy of data returned? Thank you God first.

Started by Steward at November 06, 2016 - 2:59 PM

First, AJAX does not have a setTimeouts method, request.setTimeouts (20000, 20000, 50000, 100000), deleted;

function ajax_create_request(context) {
    for (var i = 0; i <requests.length; i++) {
        //if (requests[i].readyState == 4) {
        if (requests[i].obj.readyState == 4) {
            requests[i].obj.abort(); ////
            requests[i].obj.onreadystatechange = function () { };  ////Empty the original state transition function
            requests[i].context = null;
            return requests[i];
        }
    }
 
    var pos = requests.length;
 
    requests[pos] = Object();
    requests[pos].obj = new XMLHttpRequest();
    requests[pos].context = context;
 
    return requests[pos];
}


And you import jQuery, why would you please write Ajax library? JQuery is not on the list

Posted by Louis at November 12, 2016 - 3:42 PM

Thanks for your reply. Write your own Ajax library is due to exclude jQuery problem.
There are problems with jQuery, just try to write Ajax Library.
According to your suggestion. At the beginning of operation data and sequence number is corresponding to the.
Repeated page refresh a few times. The data still mixed up:
1: [{"id":1002,"value":1002,"text":""}] tables and chairs 2: 3: [{" Id ": 1001," value ": 1001," text ":" helo computer"}]

Posted by Steward at November 27, 2016 - 3:48 PM

The jquery-1.3.2.min.js version is too old, or at least more than 1.4+. .

Here I tested did not return your problems, you are not the first database operation, direct return where parameters have a look, if not insane is your logic

select.ashx

        string where=Request.QueryString["where"];
        Response.Write(where);
        Response.End();

Posted by Louis at December 01, 2016 - 4:21 PM

When submitted no disorder, execution is indeed wher does not. Here, that logic is logic refers to data operating it?

Posted by Steward at December 16, 2016 - 4:37 PM

Check your C# code is it right? With what to cache, data sharing, different request returns the same data.

Posted by Louis at December 31, 2016 - 4:41 PM