Da_Chris
Erfahrenes Mitglied
Wie der Titel schon besagt bekomme ich nur responseText aber nicht responseXML zurück.
Vielleicht habe ich was übersehen wäre jedenfalls für schnelle Hilfe echt dankbar.
ajax.js
Anwendung:
Vielleicht habe ich was übersehen wäre jedenfalls für schnelle Hilfe echt dankbar.
ajax.js
Code:
var ajax_id = 0;
AJAX=function(args)
{
var ajax = args;
//set ajax id
ajax.id = ajax_id++;
ajax.log = '';
ajax.request = function()
{
var now = new Date();
try{
//creating XMLHttpRequest-Object
this.xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
//set transfermode
this.method=(typeof this.method == 'undefined') ? 'GET' : this.method;
this.log += 'ajax.method:\t\t\t\t'+this.method+'\n';
//preparing data
this.data=(this.data || '');
this.log += 'ajax.data:\t\t\t\t'+this.data+'\n';
//preparing postData
this.postData=(this.method=='GET') ? null : this.data;
this.log += 'ajax.postData:\t\t\t'+this.postData+'\n';
//preparing URL
this.URL=(this.method=='POST' || this.data=='') ? this.URL : ( (this.URL.indexOf('?')==-1) ? this.URL+'?'+this.data : this.URL+'&'+this.data );
this.log += 'ajax.URL:\t\t\t\t\t'+this.URL+'\n';
//creating timestamp
this.timestamp = now.getTime();
this.log += 'ajax.timestamp:\t\t\t'+this.timestamp+'\n';
//initializing request
this.xhr.open(this.method,this.URL,true);
//set post header
if(this.method=='POST')
{
this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.xhr.setRequestHeader("Content-length", this.postData.length);
this.xhr.setRequestHeader("Connection", "close");
}
//disabling cache if nocache is set
if(!typeof this.nocache || this.nocache)
{
this.xhr.setRequestHeader("If-Modified-Since", "Thu, 01 Jan 1970 00:00:00 GMT");
}
//targeting callback function
if(typeof this.callback == 'function')
{
this.xhr.onreadystatechange =
function()
{
var readyState=ajax.xhr.readyState;
var status=(readyState==4)?ajax.xhr.status:null
ajax.log += 'ajax.xhr.readyState:\t\t'+readyState+'\n';
ajax.log += 'ajax.xhr.status:\t\t\t'+status+'\n';
ajax.log += 'ajax.xhr.statusText:\t\t'+ajax.xhr.statusText+'\n';
ajax.log += 'ajax.xhr.responseText:\t\t'+ajax.xhr.responseText+'\n';
ajax.log += 'ajax.xhr.responseXML:\t\t'+ajax.xhr.responseXML+'\n';
if(readyState==4 && status==200)
{
ajax.callback(ajax,readyState,status);
}
};
}
else if(typeof this.destination == 'object')
{
this.xhr.onreadystatechange =
function()
{
var readyState=ajax.xhr.readyState;
var status=(readyState==4)?ajax.xhr.status:null
ajax.log += 'ajax.xhr.readyState:\t\t'+readyState+'\n';
ajax.log += 'ajax.xhr.status:\t\t\t'+status+'\n';
ajax.log += 'ajax.xhr.statusText:\t\t'+ajax.xhr.statusText+'\n';
ajax.log += 'ajax.xhr.responseText:\t\t'+ajax.xhr.responseText+'\n';
ajax.log += 'ajax.xhr.responseXML:\t\t'+ajax.xhr.responseXML+'\n';
if(readyState==4 && status==200)
{
ajax.destination.innerHTML = ajax.xhr.responseText;
}
};
}
//send request
// this.log += 'xhr.getAllResponseHeaders:\t\t'+xhr.getAllResponseHeaders()+'\n';
this.xhr.send(this.postData);
this.log += 'xhr.send()\n';
}
//errorhandling
catch(e)
{
try{
this.log += 'ERROR:AJAX.request()\n';
alert(this.log);
}
catch(e){}
}
}
//return ajax object
return ajax;
}
Anwendung:
Code:
function sendRequest(args){
//var args = {'method':'POST','data':'','URL':'ajax.php','nocache':true,'destination':document.getElementById('x')};
AJAX(args).request();
}
function ajax_catch(ajax,readyState,status)
{
//readyState = ajax status
//status = http status
if(readyState==4 && status==200)
{
insert_person(ajax.xhr.responseText);
}
}
var pCount = 0;
function person_remove(id)
{
var child = document.getElementById(id);
document.getElementById("persons").removeChild(child);
}
function insert_person(responseText)
{
pCount++;
var newChild = document.createElement("div");
var attrId = document.createAttribute("id");
attrId.nodeValue = "p"+pCount;
var attrClass = document.createAttribute("class");
attrClass.nodeValue = "person";
newChild.setAttributeNode(attrId);
newChild.setAttributeNode(attrClass);
newChild.innerHTML = responseText;
document.getElementById('persons').appendChild(newChild);
}
function person_add(type)
{
var args = {'method':'POST','data':'action=addPerson&type='+type,'URL':'ajax.php','nocache':true,'callback':ajax_catch};
sendRequest(args);
}