ajax - jQuery:如何为 `timeout` jsonp'...` 启用 `$.ajax({dataType:'?有什么解决办法吗?

标签 ajax jquery timeout jsonp

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 .

最佳答案

这是我使用 setTimeoutclearTimeout 的解决方案 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/

相关文章:

超时 XMLHttpRequest

php - AJAX - JSON 链式动态 MySQL Select 元素不适用于文本,但可以很好地处理数值

javascript - 通过 AJAX 在 Wordpress 中动态更改导航链接(下一个和上一个)

jQuery 点击() : Change arguments of a function after it has been executed

javascript - 学位文本需要在javascript中对齐

multithreading - 增加CFML中线程的请求超时

javascript - 如何判断nodejs child_process是否因超时而失败?

javascript - 重置表单元素而不重新加载页面

javascript - 是否可以检测网页上所有异步任务的完成情况?

javascript - Ajax 发布后页面重新加载