i wszystko jest OK - poza tym, że w jednej i tylko w jednej (akurat nie o IE chodzi, a o SeaMonkey) nie działa
chodzi o fragment
this.xmlHttp.onreadystatechange = function() { tmpObj.getResponse(); }
nie mam pojęcia co i jak zrobić aby w SM zaskoczyło
Biblioteka wygląda tak
function Ajax () { this.xmlHttp = false; this.ajaxResult = 0; this.dataStr = false; this.requestResult; this.paramLen = 0; this.errorTxt ='no error'; function createXmlHttpRequestObject(request) { if(!request || typeof request=='undefined') { try { request = new XMLHttpRequest(); } catch(e) { var XmlHttpVersions = new Array( "MSXML2.XMLHTTP.7.0", "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"); for (var i=0; i<XmlHttpVersions.length && !request; i++) { try { request = new ActiveXObject(XmlHttpVersions[i]); } catch (e) { } } } } return request; }; function encodeDataComponents(dataComponents) { var dataString = ' '; if(!dataComponents || typeof dataComponents == 'undefined') { return dataString; } if(typeof dataComponents.elements == 'undefined') return encodeURI(dataString+dataComponents); var params=dataComponents.elements; this.paramLen = params.length; if(this.paramLen==0) { return dataString; } for(n=0;n<this.paramLen;n++) { if(params.elements[n].type=='checkbox') { dataString += "&"+ params.elements[n].name + "=" + encodeURIComponent(params.elements[n].checked); } else if(params.elements[n].type=='radio') { if(params.elements[n].checked) { dataString += "&"+ params.elements[n].name + "=" + encodeURIComponent(params.elements[n].value); } } else if(params.elements[n].type=='select-multiple') { for(i=0; i<params.elements[n].options.length; i++) { dataString += "&"+ params.elements[n].name + "=" + encodeURIComponent(params.elements[n].options[m].value) } } else { dataString += "&"+ params.elements[n].name + "=" + encodeURIComponent(params.elements[n].value); } } return dataString; }; this.getAjax = function() { this.xmlHttp = createXmlHttpRequestObject(this.xmlHttp); this.ajaxResult = 2; if(!this.xmlHttp) this.ajaxResult = -11; return this.xmlHttp; }; this.getRequestData = function(data) { this.dataStr = encodeDataComponents(data); this.ajaxResult = 3; if(!this.dataStr) this.ajaxResult = -12; }; this.sendRequest = function(url,data,method,asynchro) { this.getAjax(); if(this.ajaxResult==2) { if(method!='POST' && method!='GET') method='GET'; if(asynchro != true && asynchro != false) asynchro = true; this.getRequestData(data); if(this.ajaxResult==3) { if(method == 'GET') { var urlSend = url + this.dataStr; var sendetVar = null; } else { var urlSend = url; var sendetVar = this.dataStr; } try { this.xmlHttp.open(method, urlSend, asynchro); this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8"); if(this.paramLen>0) this.xmlHttp.setRequestHeader("Content-length",this.paramLen); var tmpObj = this; this.xmlHttp.onreadystatechange = function() { tmpObj.getResponse();//tu jest problem nie wykonuje się ta funkcja } this.xmlHttp.send(sendetVar); } catch(e) { this.errorTxt = e; this.ajaxResult = -1; } } } else this.ajaxResult = -2; }; this.action = function(){}; this.getResponse = function() { if (this.xmlHttp.readyState == 4) { if (this.xmlHttp.status == 200) { var tmp = this; this.requestResult = this.xmlHttp.responseText; tmp.action(); this.ajaxResult=1; } else this.ajaxResult = -22; } else this.ajaxResult=-23; }; this.getAjaxResult = function() { return this.ajaxResult; }; this.getRequestResult = function() { return this.requestResult; }; this.getErrorTxt = function() { return this.errorTxt; }; }
a jej użycie tak
var url ='http://localhost/projekt/index.php?action=action1'; var ajaxObj = new Ajax(); ajaxObj.sendRequest(url,'','GET',false); var ajaxRetVal = ajaxObj.getAjaxResult(); if(ajaxRetVal==1) { var ajaxRet = ajaxObj.getRequestResult(); //i tutaj z rezultatem mozna zrobić co się chce }
rozwiązałem ten problem
teraz działa wszędzie (sprawdzone pod SM, FF, IE, Operą)
podczas użycia nie należy pobierać tego co zwróciło żądanie za pomocą funkcji a bezpośrednio
czyli zamiast
var ajaxRet = ajaxObj.getRequestResult();
ma być
var ajaxRet = ajaxObj.xmlHttp.responseText;
czemu tak jest - nie mam pojęcia, najważniejsze, że działa