最近在写AJAX时候 发现一些小问题
对于AJAX缓存去除方法 可以
$.ajax({
type:"POST"
url: URL,
cache:false,
dataType:"html",
success:function(msg){
alert(msg);
}
});
关于同步异步问题
$.ajax({
type:"POST"
url: URL,
async:false,
dataType:"html",
success:function(msg){
alert(msg);
}
});
AJAX即“ Asynchronous JavaScript and XML”,译为异步的JS和XML 因此同步异步 都是基于异步的基础上的,下面我们做一个测试
function test(){
$.ajax({
type:"POST",
url:URL,
async:false,
dateType:"html",
success:function(msg){
console.log(1);
},
error:function(err){
console.log(err);
}
}):
console.log(2);
}
结果:1 2
function test(){
$.ajax({
type:"POST",
url:URL,
async:false,
dateType:"json",
success:function(msg){
console.log(1);
},
error:function(){
console.log('error');
}
}):
console.log(2);
}
结果: 2 error 我们可以通过这段简单的代码可以知道,现在已经是同步了,为什么是这样的原因呢?如果改成
$.ajaxSettings.async = false;
$.getJSON(URL, {}, function(msg) {
console.log(1);
});
console.log(2);
结果:1 2
个人觉得是可能是jquery.js代码还不够完善吧