0

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 :)

nanobash
  • 5,419
  • 7
  • 38
  • 56
  • no you don't lose data in post requests. But packet loss may occur though. That's anyway why there's a responsecode ... (http status) – Sebas May 29 '12 at 20:44
  • I have replaced now and it doesn't giving alerts , but ajax script working , it is sending data to mysql database just fine , i can not understand what is going on :( – nanobash May 29 '12 at 20:56
  • also there is something: it's probably not the problem, but only if you send by post you need to send the params. If you send by GET, the params should be added up to the file url, and then send(null); – Sebas May 29 '12 at 21:57
  • Could you try your script under Firefox or Chrome please? – Sebas May 29 '12 at 22:01
  • I've tried and it doesn't do anything, doesn't alerting ? Have you tried too ? Nothing alerting :( – nanobash May 30 '12 at 07:39
  • 1
    by the way the line `this.http.send(this.params);` should be AFTER you set the `onreadystatechange` function – Sebas May 30 '12 at 11:55

1 Answers1

1
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.

Sebas
  • 21,192
  • 9
  • 55
  • 109
  • please double check syntax of responseXML, I'm not sure of capital letters – Sebas May 29 '12 at 20:10
  • I was trying to assign in AjaxConstruct this.flag=false, and in script when was ok was writting this.flag = true , otherwise this.flag = false, but this was not working ( how i think because of this.flag was assigned to "false" in AjaxConstruct function it was not overwritten after that. So how I thought with post method doesn't matter data amount yes ? :) – nanobash May 29 '12 at 20:11
  • i don't understand, is it related to the main question? – Sebas May 29 '12 at 20:15
  • in a POST query, the limit is server side: see this post: http://stackoverflow.com/questions/2364840/what-is-the-size-limit-of-a-post-request – Sebas May 29 '12 at 20:18
  • no other , just i want when invoking ajaxOkResponse() method just alert("ok") and the same in ajaxErrorResponse() method, but it doesn't invoking methods , but the ajax engine works fine :( – nanobash May 29 '12 at 20:46