Touched up the XMLHttpRequest code (fixed some bugs, fixed formatting, etc.).

This commit is contained in:
John Resig 2007-07-09 00:48:16 +00:00
parent c4eddea7c3
commit de71a046e6

View file

@ -469,6 +469,7 @@ var window = this;
} }
// XMLHttpRequest // XMLHttpRequest
// Originally implemented by Yehuda Katz
window.XMLHttpRequest = function(){ window.XMLHttpRequest = function(){
this.headers = {}; this.headers = {};
@ -478,7 +479,8 @@ var window = this;
XMLHttpRequest.prototype = { XMLHttpRequest.prototype = {
open: function(method, url, async, user, password){ open: function(method, url, async, user, password){
this.readyState = 1; this.readyState = 1;
if(async) this.async = true; if (async)
this.async = true;
this.method = method || "GET"; this.method = method || "GET";
this.url = url; this.url = url;
this.onreadystatechange(); this.onreadystatechange();
@ -488,61 +490,80 @@ var window = this;
}, },
getResponseHeader: function(header){ }, getResponseHeader: function(header){ },
send: function(data){ send: function(data){
var makeRequest = function() { function makeRequest(){
var url = new java.net.URL(this.url); var url = new java.net.URL(this.url),
var connection = url.openConnection(); connection = url.openConnection();
// Add headers to Java connection // Add headers to Java connection
for(header in this.headers) connection.addRequestProperty(header, this.headers[header]); for (var header in this.headers)
connection.addRequestProperty(header, this.headers[header]);
connection.connect(); connection.connect();
// Stick the response headers into responseHeaders // Stick the response headers into responseHeaders
for (i=0; ; i++) { for (i=0; ; i++) {
headerName = connection.getHeaderFieldKey(i); var headerName = connection.getHeaderFieldKey(i);
headerValue = connection.getHeaderField(i); var headerValue = connection.getHeaderField(i);
if (!headerName && !headerValue) break; if (!headerName && !headerValue) break;
if(headerName) this.responseHeaders[headerName] = headerValue; if (headerName)
this.responseHeaders[headerName] = headerValue;
} }
this.readyState = 4; this.readyState = 4;
this.status = parseInt(connection.responseCode); this.status = parseInt(connection.responseCode);
this.statusText = connection.responseMessage; this.statusText = connection.responseMessage;
var stream = new java.io.InputStreamReader(connection.getInputStream());
var buffer = new java.io.BufferedReader(stream); var stream = new java.io.InputStreamReader(
var line; connection.getInputStream()),
while((line = buffer.readLine()) != null) this.responseText += line; buffer = new java.io.BufferedReader(stream),
line;
while ((line = buffer.readLine()) != null)
this.responseText += line;
try { try {
this.responseXML = new DOMDocument(this.responseText); this.responseXML = new DOMDocument(this.responseText);
} catch(e) { } catch(e) {
this.responseXML = null; this.responseXML = null;
} }
this.onreadystatechange(); this.onreadystatechange();
};
if(this.async) {
var requestThread = (new java.lang.Thread(new java.lang.Runnable({
run: makeRequest
})));
requestThread.start();
} }
else makeRequest();
if (this.async)
(new java.lang.Thread(new java.lang.Runnable({
run: makeRequest
}))).start();
else
makeRequest();
}, },
abort: function(){}, abort: function(){},
onreadystatechange: function(){}, onreadystatechange: function(){},
getResponseHeader: function(header){ getResponseHeader: function(header){
if(this.readyState < 3) throw new Error("INVALID_STATE_ERR"); if (this.readyState < 3)
throw new Error("INVALID_STATE_ERR");
else { else {
var returnedHeaders = []; var returnedHeaders = [];
for(rHeader in this.responseHeaders) { for (var rHeader in this.responseHeaders) {
if(rHeader.match(new Regexp(header, "i"))) returnedHeaders.push(this.responseHeaders[rHeader]); if (rHeader.match(new Regexp(header, "i")))
returnedHeaders.push(this.responseHeaders[rHeader]);
} }
if(returnedHeaders != []) return returnedHeaders.join(", ");
if (returnedHeaders.length)
return returnedHeaders.join(", ");
} }
return null; return null;
}, },
getAllResponseHeaders: function(header){ getAllResponseHeaders: function(header){
if(this.readyState < 3) throw new Error("INVALID_STATE_ERR"); if (this.readyState < 3)
throw new Error("INVALID_STATE_ERR");
else { else {
returnedHeaders = []; var returnedHeaders = [];
for(var header in this.responseHeaders) {
returnedHeaders += (header + ": " + this.responseHeaders[header]); for (var header in this.responseHeaders)
} returnedHeaders.push( header + ": " + this.responseHeaders[header] );
return returnedHeaders.join("\r\n"); return returnedHeaders.join("\r\n");
} }
}, },