日期:2014-05-16 浏览次数:20499 次
var isOK = true;
function createXmlHttpRequest(){
if(window.ActiveXObject){ //如果是IE浏览器
return new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){ //非IE浏览器
return new XMLHttpRequest();
}
}
var xmlReq = createXmlHttpRequest();//创建一个xmlhttprequest对象
function testAsynRequest() {
var url = "http://xxx.com/test.ashx?ajax=xxuusks";
xmlReq.open("post", url, true);
xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlReq.onreadystatechange = function () {
if (xmlReq.readyState == 4) {
if (xmlReq.status == 200) {
var jsonData = eval('(' + xmlReq.responseText + ')');
alert(jsonData.message);
}
else if (xmlReq.status == 404) {
alert("Requested URL is not found.");
} else if (xmlReq.status == 403) {
alert("Access denied.");
} else {
alert("status is " + xmlReq.status);
}
}
isOK = false;
};
xmlReq.send(null);
}
testAsynRequest(); //这里始终没有被响应
while (isOK) { } //这里被先执行了
alert("完成继续下一个执行");
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function task1(){
$.ajax({url:"",
success:function(){
alert("task1 异步success完成,点击后执行下一项任务");
doOne();
}
});
}
function task2(){
$.ajax({url:"",