ajax同步请求和异步请求
ajax同步和异步的差异, 先看2段代码:
代码一:
Synchronize = function(url,param) { function createXhrObject() { var http; var activeX = [ "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ]; try { http = new XMLHttpRequest; } catch (e) { for (var i = 0; i < activeX.length; ++i) { try { http = new ActiveXObject(activeX[i]); break; } catch (e) {} } } finally { return http; } } var conn = createXhrObject(); conn.open("POST", url, false);//ajax同步 conn.send(param); var strReturn = conn.responseText; alert("1"); if (strReturn != "") { return Ext.decode(conn.responseText); } else { return null; } alert("2"); };
代码二:
Ajax 同步请求方式: Synchronize = function(url,param) { function createXhrObject() { var http; var activeX = [ "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ]; try { http = new XMLHttpRequest; } catch (e) { for (var i = 0; i < activeX.length; ++i) { try { http = new ActiveXObject(activeX[i]); break; } catch (e) {} } } finally { return http; } } var conn = createXhrObject(); conn.open("POST", url, true);//ajax异步 conn.send(param); var strReturn = conn.responseText; alert("1"); if (strReturn != "") { return Ext.decode(conn.responseText); } else { return null; } alert("2"); };
同步和异步的差异如下:
conn.open('POST',Url,true); // ajax异步
conn.open('POST',Url,false); // ajax同步
对于代码二,为异步的ajax请求,执行结果为:先执行alert(2)再执行alert(1), 异步的意思就是说一旦conn.open请求一发出,前端不去等待它的响应便执行后面的代码,所以alert(2)先执行了,然后当响应response到达以后才执行alert(1);
对于代码一,为同步的ajax请求,执行结果为:先执行alert(1)再执行alert(2), 同步的意思就是说一旦conn.open请求一发出,前端就去等待它的响应,响应完成以后,alert(1)先执行了,然后alert(2);
相关推荐
坚持着执着 2020-07-16
坚持着执着 2020-06-14
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
jiaguoquan00 2020-07-07
李永毅 2020-07-05