关于async
var $={
ajax:function(options){
var xhr=null, //XMLHttpRequest对象
url=options.url, //url地址
method=options.method || 'get',//传输方式,默认为get
async=typeof(options.method)==="undefined" ? true:options.async;
console.log(async);
if(typeof XMLHttpRequest != "undefined"){
xhr = new XMLHttpRequest();
}else if(typeof ActiveXObject != "undefined"){
//将所有可能出现的ActiveXObject版本放在一个数组中
var xhrArr=['Microsoft.XMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP.2.0'];
//遍历创建XMLHttpRequest对象
var len=xhrArr.length;
for(var i=0;i<len;i++){
try{
//创建XMLHttpRequest对象
xhr=new ActiveXObject(xhrArr[i]);
break;
}
catch(ex){
}
}
}else{
throw new Error('No XHR object availabel.')
}
}
}
$.ajax({
url:"",
method:"post",
async:false
})老师出现一个费解的情况,当我33行method:"post",这个请求方式不存在的时候,我打印async都是true,但是method:"post",有的话,打印的结果就是false,这之间有什么联系方式吗???
22
收起
正在回答 回答被采纳积分+1
2回答
Aurora_Meteor
2020-03-29 12:47:57
是你判断的地方写错了呀,你写的是
async=typeof(options.method)==="undefined" ? true:options.async;
正确的应该是
async = typeof (options.async) === 'undefined' ? true : options.async;
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星