【问题标题】:Redirect to another domain using jquery ajax [duplicate]使用jquery ajax重定向到另一个域[重复]
【发布时间】:2012-09-22 06:51:38
【问题描述】:

可能重复:
jquery (ajax) redirect to another domain

我有以下格式的 jQuery Ajax 请求:

$.ajax({
        url: 'xyz.com',
        cache: false,
        success: function(data){}
      );

如果请求失败(服务器没有响应),我如何重定向到另一个域。

我通过以下方式使用 jquery getJson 做了同样的事情:

$.getJSON('xyz.com',function(result) {})
 .error(
    function(xhr, textStatus, errorThrown){
        if (xhr.status == 500) {
            $.getJSON('zxy.com', function(result){});
        }
    });
});

【问题讨论】:

  • window.location.replace("stackoverflow.com");
  • @vishnu 我不太确定你的问题是否可以理解
  • This 回答会帮助你。

标签: jquery json


【解决方案1】:

演示http://jsfiddle.net/dYgfG/

代码

var domain = "http://xyz.com";

try {
    $.ajax({
        url: domain,
        cache: false,
        success: function(data){},
        error: function() {
            alert('ajax error: so redirecting');
            redirectUser();
        }
    });
}
catch(e) {
    alert('exception: so redirecting');
    redirectUser();
}

function redirectUser() {
    window.location.href = domain;        
}

【讨论】:

  • 不客气。新用户?如果答案对您有帮助,请使用答案标题旁边的绿色勾号将其标记为“已接受”。
【解决方案2】:

我认为你应该使用:crossDomain ajax 属性。检查jquery apilink

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2019-11-25
    • 2010-10-14
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多