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,82 +469,103 @@ var window = this;
} }
// XMLHttpRequest // XMLHttpRequest
// Originally implemented by Yehuda Katz
window.XMLHttpRequest = function(){ window.XMLHttpRequest = function(){
this.headers = {}; this.headers = {};
this.responseHeaders = {}; this.responseHeaders = {};
}; };
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.method = method || "GET"; this.async = true;
this.url = url; this.method = method || "GET";
this.onreadystatechange(); this.url = url;
this.onreadystatechange();
}, },
setRequestHeader: function(header, value){ setRequestHeader: function(header, value){
this.headers[header] = value; this.headers[header] = value;
}, },
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
for(header in this.headers) connection.addRequestProperty(header, this.headers[header]); // Add headers to Java connection
connection.connect(); for (var header in this.headers)
// Stick the response headers into responseHeaders connection.addRequestProperty(header, this.headers[header]);
for(i=0;;i++) {
headerName = connection.getHeaderFieldKey(i); connection.connect();
headerValue = connection.getHeaderField(i);
if(!headerName && !headerValue) break; // Stick the response headers into responseHeaders
if(headerName) this.responseHeaders[headerName] = headerValue; for (i=0; ; i++) {
} var headerName = connection.getHeaderFieldKey(i);
this.readyState = 4; var headerValue = connection.getHeaderField(i);
this.status = parseInt(connection.responseCode); if (!headerName && !headerValue) break;
this.statusText = connection.responseMessage; if (headerName)
var stream = new java.io.InputStreamReader(connection.getInputStream()); this.responseHeaders[headerName] = headerValue;
var buffer = new java.io.BufferedReader(stream); }
var line;
while((line = buffer.readLine()) != null) this.responseText += line; this.readyState = 4;
try { this.status = parseInt(connection.responseCode);
this.responseXML = new DOMDocument(this.responseText); this.statusText = connection.responseMessage;
} catch(e) {
this.responseXML = null; var stream = new java.io.InputStreamReader(
} connection.getInputStream()),
this.onreadystatechange(); buffer = new java.io.BufferedReader(stream),
}; line;
if(this.async) {
var requestThread = (new java.lang.Thread(new java.lang.Runnable({ while ((line = buffer.readLine()) != null)
run: makeRequest this.responseText += line;
})));
requestThread.start(); try {
} this.responseXML = new DOMDocument(this.responseText);
else makeRequest(); } catch(e) {
this.responseXML = null;
}
this.onreadystatechange();
}
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)
else { throw new Error("INVALID_STATE_ERR");
var returnedHeaders = []; else {
for(rHeader in this.responseHeaders) { var returnedHeaders = [];
if(rHeader.match(new Regexp(header, "i"))) returnedHeaders.push(this.responseHeaders[rHeader]); for (var rHeader in this.responseHeaders) {
} if (rHeader.match(new Regexp(header, "i")))
if(returnedHeaders != []) return returnedHeaders.join(", "); returnedHeaders.push(this.responseHeaders[rHeader]);
} }
return null;
if (returnedHeaders.length)
return returnedHeaders.join(", ");
}
return null;
}, },
getAllResponseHeaders: function(header) { getAllResponseHeaders: function(header){
if(this.readyState < 3) throw new Error("INVALID_STATE_ERR"); if (this.readyState < 3)
else { throw new Error("INVALID_STATE_ERR");
returnedHeaders = []; else {
for(var header in this.responseHeaders) { var returnedHeaders = [];
returnedHeaders += (header + ": " + this.responseHeaders[header]);
} for (var header in this.responseHeaders)
return returnedHeaders.join("\r\n"); returnedHeaders.push( header + ": " + this.responseHeaders[header] );
}
return returnedHeaders.join("\r\n");
}
}, },
async: true, async: true,
readyState: 0, readyState: 0,