如何使用纯Javascript实现同步Ajax调用?
我需要实现同步Ajax调用机制。我已经在我的助手中实现了ajax调用功能,如下所示:如何使用纯Javascript实现同步Ajax调用?,javascript,ajax,Javascript,Ajax,我需要实现同步Ajax调用机制。我已经在我的助手中实现了ajax调用功能,如下所示: MH.helper = { ajax : function (option) { if(option !== undefined) { for(var opt in option) { this[opt] = option[opt]; } } if (window.XMLHt
MH.helper = {
ajax : function (option) {
if(option !== undefined) {
for(var opt in option) {
this[opt] = option[opt];
}
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr=new XMLHttpRequest();
} else {
// code for IE6, IE5
this.xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
MH.helper.ajax.prototype = {
// XMLHttpRequest obj
xhr : null,
// request url
url: '',
// post funciton
post: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data, true);
xhr.open("POST",this.url,true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(data);
},
// get funciton
get: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data);
xhr.open("GET",this.url+data,true);
xhr.send(data);
},
// callback when request done
complete: null
}
我还实现了Ajax原型,如下所示:
MH.helper = {
ajax : function (option) {
if(option !== undefined) {
for(var opt in option) {
this[opt] = option[opt];
}
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr=new XMLHttpRequest();
} else {
// code for IE6, IE5
this.xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
MH.helper.ajax.prototype = {
// XMLHttpRequest obj
xhr : null,
// request url
url: '',
// post funciton
post: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data, true);
xhr.open("POST",this.url,true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(data);
},
// get funciton
get: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data);
xhr.open("GET",this.url+data,true);
xhr.send(data);
},
// callback when request done
complete: null
}
有人知道如何使用Ajax函数实现同步调用吗?将
false
作为xhr.open的第三个参数传递
来源:,.传递false
作为xhr.open的第三个参数
资料来源:
如果将false
作为第三个参数而不是true
,则调用将同步执行
但我的建议是——不要。始终使用回调函数
如果将false
作为第三个参数而不是true
,则调用将同步执行
但我的建议是——不要。始终使用回调函数。不要这样做。同步请求将冻结浏览器。@SLaks我知道。但是我希望我的功能多样化。无论如何不要这样做。@monsur.hoq-FYI,AJAX中的A代表“异步”@O.O“不需要重新发明轮子,是吗?:)“但是OP只是想知道如何构建同步XHR调用,而不是整个轮子。不要这样做。同步请求将冻结浏览器。@SLaks我知道。但是我希望我的功能多样化。无论如何不要这样做。@monsur.hoq-FYI,AJAX中的A代表“异步”@O.O“不需要重新发明轮子,是吗?:)“但是OP只是想知道如何构建同步XHR调用,而不是整个轮子。错误->它会杀死大多数设备。@lin它可能不被推荐,但它不是“错误的”同意!编辑:这将杀死大多数设备。特别是移动设备:)@lin OP专门询问如何强制同步XHR–如果这是一个好主意,则不是。错误->它将杀死大多数设备。@lin可能不推荐使用它,但它不是“错误”同意!编辑:这将杀死大多数设备。特别是移动设备:)@lin OP特别询问如何强制同步XHR——如果这是一个好主意的话就不是了。