0

登録用にjavascriptでajaxoopスクリプトを作成しました。これがスクリプトです=>

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");
        }
    };

}
};

そしてそのようなオブジェクトを作成する=>

var ajax = new AjaxConstruct("POST","filename","params"); // define object
ajax.ajax(); // invoke method

すべてが完全に機能していますが、結果が正常な場合とそうでない場合に、oopスクリプトでフラグを立てる方法を知りたいだけです。また、ajaxで送信するのに一般的なデータの数に興味がありますか、それとも重要ではありませんか?たとえば、7つの入力フォームからmysqlデータベースにデータを送信しようとしていますが、このようなajaxスクリプトを使用して送信中にデータが失われる可能性はありますか?ありがとう :)

私はエラーを推測し、上記のスクリプトで修正しました、ありがとうございます:)

4

1 に答える 1

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);
}
};

ajaxconstructを呼び出すときは、次のような2つの新しい引数を渡します。

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

データ量の懸念については、GETはブラウザのアドレスバーの制限に従って制限しますが、POSTは制限しないと思います。しかし、これはhttpサーバーのオーバーヘッドの問題であり、自分自身に問いかける必要があります。

于 2012-05-29T20:06:45.557 に答える