Ich habe einen Test für meinen XMLHttpRequest-Emulator gebaut. Natürlich musste ich dafür wieder einmal ein paarBugs beheben. Darum ist hier nicht nur die HTML-Seite, von der aus der Test ausgeführt wird und das PHP-Skript, sondern auch das angepasste Javascript.
Ich bin heute vor keinem Windows-Rechner gesessen. Darum habe ich noch nicht getestet, ob es auch mit dem IE wie gewünscht funktioniert.
test.html
<HTML>
<HEAD>
<TITLE>XMLHttpRequest test</TITLE>
<SCRIPT LANGUAGE=“JavaScript” SRC=“ajaxlib.js” TYPE=“text/javascript”></script>
<script language=“JavaScript”>
function testXMLHttpRequest() {
var test = new XMLHttpRequest();
test.open(’POST’,’test.php’);
test.onreadystatechange = function() {
if( test.readyState != 4 ) return;
else alert(test.responseText);
}
test.send(’<test> test </test>’)
}
</script>
</HEAD>
<BODY>
<a href=“javascript: testXMLHttpRequest()”>test</a>
</BODY>
</HTML>
test.php
<?
$xmldoc = ‘’;
if( $_REQUEST[’XMLHttpRequestBody’] ) {
print “XMLHttpRequest emulation\n”;
$xmldoc = $_REQUEST[’XMLHttpRequestBody’];
} else {
print “Native XMLHttpRequest\n”;
$xmldoc = file_get_contents(“php://input”);
}
echo $xmldoc;
?>
ajaxlib.js
/***************************************************************************
Copyright (C) 2005 by Christof Donat
cdonat@gmx.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***************************************************************************/
// fix missing features in variuos Bowsers - this should work with most more
// or less modern Browsers.
if( ! window.XMLHttpRequest ) {
// Browsers like e.g. Mozilla and Firefox have this already
try {
// MSIE Version 5 and above has an ActiveX component; MSIE 6 has a better one
var lib = /MSIE 5/.test(navigator.userAgent) ? “Microsoft” : “Msxml2”;
var test = new ActiveXObject(lib + “.XMLHTTP”);
XMLHttpRequest = function() { return new ActiveXObject(lib + “.XMLHTTP”); }
} catch( ex ) {
// either scripting of secure ActiveX components has been turned off or this isn’t MSIE.
// This will need a Hack“
XMLHttpRequest = function() {
// Iframe Hack
var index = 1;
XMLHttpRequest = function() {
this.readyState = 0;
this.onreadystatechange = function() {};
this._private = new Object();
this._private.iframe = document.createElement(’iframe’);
index++;
this._private.getFullUrl = function() {
var tmp = this.url;
var userpass = ‘’;
if( this.user ) {
userpass = this._private.user;
if( this.password )
userpass += ‘:’+this.password;
tmp = tmp.split(’://’,2);
tmp = tmp[0] + ‘://’ + userpass + ‘@’ + tmp[1];
}
return tmp;
}
this._private.sendPost = function(body) {
var form = this._private.iframe.document.createElement(’form’);
form.action = this._private.getFullUrl();
form.method = this._private.method;
form.encoding = ‘text/xml’;
var input = this._private.iframe.document.createElement(’input’);
input.value = body;
input.name = ‘XMLHttpRequestBody’;
form.appendChild(input);
form.submit();
}
this._private.sendGet = function(body) {
var tmp = this._private.getFullUrl();
tmp += ‘?XMLHttpRequestBody=’+encodeURI(body);
this._private.iframe.src = tmp;
}
var getreadystatechangefunction = function(object) {
return function() {
object.readyState = object._private.iframe.readyState;
if( !object._private.aborted )
object.onreadystatechange();
}
}
var getloadedfunction = function(object) {
return function() {
if( object._private.aborted ) {
object._private.aborted = false;
} else {
object.responseText = object._private.iframe.innerText;
object.reponseXML = object._private.iframe.document;
}
}
}
this._private.iframe.attachEvent (”onreadystatechange“,getreadystatechangefunction(this));
this._private.iframe.attachEvent (”onload“,getloadedfunction(this));
}
XMLHttpRequest.prototype.open = function(method,url,assync,user,password) {
this._private.method = method;
this._private.url = url;
this._private.assync = assync;
this._private.user = user;
this._private.password = password;
this._private.aborted = false;
if( method.toLowerCase() == ”get“ ) this.send = this._private.sendGet;
else if( method.toLowerCase() == ”post“ ) this.send = this._private.sendPost;
else this.send = function(body) {};
}
XMLHttpRequest.prototype.send = function(body) {} // this will be changed in open()
XMLHttpRequest.prototype.abort = function() { this._private.aborted = true; this._private.iframe.src = ”about:empty" }
XMLHttpRequest.prototype.setRequestHeader = function(header,value) { this._private.requestHeader[header] = value; }
XMLHttpRequest.prototype.getResponseHeader = function(header) { return this._private.responseHeader[header]; }
XMLHttpRequest.prototype.getAllResponseHeaders = function() { return this._private.responseHeaderTxt; }
XMLHttpRequest.prototype.onreadystatechange = function() {};
// not supportet - could find no way to simulate these
XMLHttpRequest.prototype.responseStream = false;
XMLHttpRequest.prototype.setRequestHeader = new Function;
XMLHttpRequest.prototype.getResponseHeader = new Function;
XMLHttpRequest.prototype.getAllResponseHeaders = new Function;
}
}
}
Mein Test für den XMLHttpRequest-Emulator hat dann doch noch eine zentrale Schwächen offenbart: er hat nicht funktioniert. Das habe ich inzwischen behoben, wie man sehen kann.Der dazugehörige Code: ajaxlib.js: /****************
Aufgenommen: Feb 12, 20:13