admin管理员组文章数量:1391981
I need to implement synchronous Ajax calling mechanism. I already implement ajax calling function in my helper same as below :
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");
}
}
}
I also implement Ajax prototype as same as below :
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(thatplete && ( typeof thatplete === 'function' )) {
thatplete(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(thatplete && ( typeof thatplete === 'function' )) {
thatplete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data);
xhr.open("GET",this.url+data,true);
xhr.send(data);
},
// callback when request done
plete: null
}
Anyone have any idea how can I implement synchronous call using my Ajax function?
I need to implement synchronous Ajax calling mechanism. I already implement ajax calling function in my helper same as below :
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");
}
}
}
I also implement Ajax prototype as same as below :
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.plete && ( typeof that.plete === 'function' )) {
that.plete(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.plete && ( typeof that.plete === 'function' )) {
that.plete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data);
xhr.open("GET",this.url+data,true);
xhr.send(data);
},
// callback when request done
plete: null
}
Anyone have any idea how can I implement synchronous call using my Ajax function?
Share Improve this question asked Mar 17, 2014 at 19:28 monsur.hoqmonsur.hoq 1,13117 silver badges26 bronze badges 9- 4 Don't do that. Synchronous requests will freeze the browser. – SLaks Commented Mar 17, 2014 at 19:30
- 1 @SLaks I know that. But I want to diversity in my function. – monsur.hoq Commented Mar 17, 2014 at 19:33
- 2 Don't do that anyway. – SLaks Commented Mar 17, 2014 at 19:34
- @monsur.hoq - FYI, the A in AJAX stands for "Asynchronous" – O.O Commented Mar 17, 2014 at 19:53
- 1 @O.O "No need to reinvent the wheel, is there? :)" but the OP just wants to know how to build a synchronous XHR call – not the whole wheel. – Barney Commented Mar 17, 2014 at 20:15
2 Answers
Reset to default 6xhr.open("POST",this.url,true)
If you pass false
as the 3rd parameter instead of true
- the call will be performed synchronously.
But my advice - don't. Use callback functions always.
Pass false
as the third argument of xhr.open
.
Sources: Specification, MDN article.
本文标签: How can implement synchronous Ajax call using pure JavascriptStack Overflow
版权声明:本文标题:How can implement synchronous Ajax call using pure Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744702524a2620643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论