我在javascript中编写了ajax oop脚本进行注册。这是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");
}
};
}
};
并创建类似于=>
的对象var ajax = new AjaxConstruct("POST","filename","params"); // define object
ajax.ajax(); // invoke method
一切都运行得很完美,但我想知道如果结果很好而且不是时候我怎么能在oop脚本中标记? 而且我对使用ajax发送的数据有多少感兴趣或者不重要?例如,我试图从七个输入表单向mysql数据库发送数据,是否有机会在使用这样的ajax脚本发送过程中丢失数据?谢谢:))
我猜错了并在上面的脚本中纠正了,谢谢男人:)
答案 0 :(得分: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服务器开销的问题,你应该问自己。