jQuery:如何为 $.ajax({dataType:'jsonp'...
启用 timeout
?有什么解决方案吗?http://jsfiddle.net/laukstein/2wcpU/4
$.ajax({
type:"GET",
url:'http://lab.laukstein.com/ajax-seo/.json',
dataType:'jsonp',
timeout:200, // Not working with dataType:'jsonp'
success:function(data){$('#content').html(data.content);},
error:function(request,status,error){$('#content').html('request failed');}
});
我不喜欢为此使用某些插件,例如 http://code.google.com/p/jquery-jsonp .
最佳答案
这是我使用 setTimeout
和 clearTimeout
的解决方案 http://jsfiddle.net/laukstein/2wcpU/7/
$('#content').ajaxStart(function(){
$(this).html('Loading...');
});
var timer=window.setTimeout(function(){
$('#content').html('Loading seems to be taking a while. Try again.');
},2000);
$.ajax({
type:"GET",
url:'http://lab.laukstein.com/ajax-seo/.json',
dataType:'jsonp',
success:function(data){
window.clearTimeout(timer);
$('#content').html(data.content);
},
error:function(){
window.clearTimeout(timer);
$('#content').html('The request failed. Try to refresh page.');
}
});
关于ajax - jQuery:如何为 `timeout` jsonp'...` 启用 `$.ajax({dataType:'?有什么解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679856/