Compare Revisions

Getting Started

Change Revisions

Revision 101373:

Revision 101373 by Bcarideo on

Revision 101374:

Revision 101374 by Swamp Ig on

Title:
Getting Started
Getting Started
Slug:
Talk:AJAX/Getting_Started
Talk:AJAX/Getting_Started
Content:

Revision 101373
Revision 101374
tt64    <hr>
65    <p>
66      This would fail if multiple ajax requests are being process
 >ed at the same time, wouldn't it be better to do somthing like:
67    </p>
68    <pre>
69&lt;script type="text/javascript" language="javascript"&gt;
70 
71 
72    function makeRequest(url) {
73 
74        var http_request = false;
75 
76        if (window.XMLHttpRequest) { // Mozilla, Safari,...
77            http_request = new XMLHttpRequest();
78            if (http_request.overrideMimeType) {
79                http_request.overrideMimeType('text/xml');
80                // See note below about this line
81            }
82        } else if (window.ActiveXObject) { // IE
83            try {
84                http_request = new ActiveXObject("Msxml2.XMLHTTP"
 >);
85            } catch (e) {
86                try {
87                    http_request = new ActiveXObject("Microsoft.X
 >MLHTTP");
88                } catch (e) {}
89            }
90        }
91 
92        if (!http_request) {
93            alert('Giving up :( Cannot create an XMLHTTP instance
 >');
94            return false;
95        }
96        http_request.onreadystatechange = function() { 
97           alertContents(httpRequest); 
98        };
99        http_request.open('GET', url, true);
100        http_request.send(null);
101 
102    }
103 
104    function alertContents(http_request) {
105 
106        if (http_request.readyState == 4) {
107            if (http_request.status == 200) {
108                alert(http_request.responseText);
109            } else {
110                alert('There was a problem with the request.');
111            }
112        }
113 
114    }
115&lt;/script&gt;
116</pre>
117    <p>
118      That way the request isn't stored in a global variable. I h
 >aven't tested this code btw, but it seems reasonable to me.
119    </p>

Back to History