I've written ajax oop script in javascript for registration. here is script =>
function AjaxConstruct(method,file,params){
this.method = method;
this.file = file;
this.params = params;
this.http = false;
}
AjaxConstruct.prototype.ajax = function(){
if (window.XMLHttpRequest){
this.http = new XMLHttpRequest();
} else {
this.http = new ActiveXObject("Microsoft.XMLXHTTP");
}
if (this.http){
this.http.open(this.method,this.file,true);
if (this.method==="POST"){
this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.http.send(this.params);
this.http.onreadystatechange = function(){
if (this.http.readyState==4 && this.http.status==200){
alert("ok");
} else {
alert("no");
}
};
}
};
and creating object like that =>
var ajax = new AjaxConstruct("POST","filename","params"); // define object
ajax.ajax(); // invoke method
everything is working perfectly but just i want to know how can i flag in oop script when result is fine and when it is not ? And also I'm interested in how many data is common to send with ajax or doesn't it matter ? For example I am trying to send data to mysql database from seven input form, Is any chance to lost data during sending with ajax script like this ? Thanks :)
I've guessed error and corrected in script above , thanks man :)
function AjaxConstruct(method,file,params,okcallback,notokcallback){
this.method = method;
this.file = file;
this.params = params;
this.http = false;
this.okresp = okcallback;
this.notokresp = notokcallback;
}
AjaxConstruct.prototype.ajax = function(){
if (window.XMLHttpRequest){
this.http = new XMLHttpRequest();
} else {
this.http = new ActiveXObject("Microsoft.XMLXHTTP");
}
if (this.http){
this.http.open(this.method,this.file,true);
if (this.method==="POST"){
this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.http.onreadystatechange = function(){
if (this.http.readyState==4 && this.http.status==200) {
this.okresp(this.http.responseText, this.http.responseXML);
} else {
this.notokresp(this.http.responseText, this.http.responseXML);
}
};
this.http.send(this.params);
}
};
When you call your ajaxconstruct, pass the 2 new arguments like this:
function myokfunction(I_sResponseText, I_oResponseXML) { /* your ok code here */}
function mynotokfunction(I_sResponseText, I_oResponseXML) { /* your not ok code here */}
var ajax = new AjaxConstruct("POST","filename","params", myokfunction, mynotokfunction);
About your concern of the amount of data, GET will limit you according to the browser adress bar limit while POST won't, I think. But then this is more a question of http server overhead that you should ask yourself.