2012-05-29 155 views
0

我已經在javascript中註冊了ajax操作腳本。這裏是腳本=>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腳本發送期間是否有機會丟失數據?謝謝:)

我已經猜到誤差和上面的腳本糾正,感謝的人:)

+0

不,你不會丟失發佈的請求中的數據。但是可能會發生數據包丟失。這就是爲什麼有一個響應代碼...(http狀態) – Sebas

+0

我現在已經替換,它不提供警報,但ajax腳本工作,它發送數據到MySQL數據庫就好了,我不明白是怎麼回事: ( – tnanoba

+0

也有一些東西:它可能不是問題,但只有當你發送郵件時你需要發送參數。如果你通過GET發送,params應該被添加到文件url,然後發送(null) ; – Sebas

回答

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服務器開銷的問題,你應該問問自己。

+0

請仔細檢查responseXML的語法,我不確定大寫字母 – Sebas

+0

我試圖在AjaxC中分配onstruct this.flag = false,並且在腳本中當確定寫入this.flag = true時,否則this.flag = false,但這不起作用(我認爲這是因爲this.flag在AjaxConstruct中被賦予「false」函數之後它不會被覆蓋。那麼我如何用post方法思考數據量無關緊要呢? :) – tnanoba

+0

我不明白,是否與主要問題有關? – Sebas