加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dojo.xhrGet()同步请求的方法

发布时间:2020-12-16 21:59:09 所属栏目:百科 来源:网络整理
导读:官方详细讲解: http://dojotoolkit.org/reference-guide/1.10/dojo/xhrGet.html dojo.xhrGet()请求可以通过设置参数sync:true; 保证发出同步请求,即只有server发出response,执行完回调函数后,才能执行其他操作。 如果不设置sync参数,默认为异步请求,

官方详细讲解: http://dojotoolkit.org/reference-guide/1.10/dojo/xhrGet.html


dojo.xhrGet()请求可以通过设置参数sync:true; 保证发出同步请求,即只有server发出response,执行完回调函数后,才能执行其他操作。

如果不设置sync参数,默认为异步请求,即请求发出后,前端还可以发出其他请求,执行其他操作,然后等server准备好数据后,再调用回调函数load 。


NOTE:参数sync:true; 这样设置后,能够保证前端得到server的response后,再执行其他方法, 所以能保证同步性。

但这样设置的弊端在于:若请求不成功,会block在这个请求上,不能进行其他操作。

例子:

 var xhrArgs = {
					url: "www.baidu.com",handleAs: "xml",preventCache: true,sync : true,load: dojo.hitch(this,function(response,ioargs){
						var responseStatus = ioargs.xhr.status;
						var responseText = ioargs.xhr.responseText;
						this.logger.debug("responseStatus= " + responseStatus);
						if(responseText){
							var entries = response.getElementsByTagName("entry")[0].childNodes;
						}
					}),error: dojo.hitch(this,function(error,ioargs) {			
						this.logger.debug("in error():" +error.message);
						
						var responseStatus = ioargs.xhr.status;
						this.logger.debug("responseStatus= " + responseStatus);
						
					})
				};
				dojo.xhrGet(xhrArgs); 
			}
上述例子不能执行,因为www.baidu.com返回的不是xml, 只是讲解sync的用法。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读