0

我已经在 javascript 中编写了 ajax oop 脚本以进行注册。这是脚本=>

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 发送多少数据很常见或没关系感兴趣?例如,我正在尝试从七个输入表单向 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 回答