【问题标题】:Loading external website view jQuery ajax [duplicate]加载外部网站视图jQuery ajax [重复]
【发布时间】:2013-05-17 08:19:20
【问题描述】:

目前,我使用下面的 js 行在我的 HTML 页面中加载网站。

window.location = 'http://example.com';

如何使用 jQuery / AJAX 仅在 http://example.com 返回 HTTP 状态 200 时加载,并在出现错误时显示另一个页面,例如 ERROR 500、504....等。

【问题讨论】:

标签: javascript jquery ajax


【解决方案1】:

window.location 重定向到指定的 url,不会将 uri 加载到您的 HTML 页面中。

根据您的问题: jQuery Ajax error handling, show custom exception messages 展示了如何处理 ajax 错误。在您的情况下,错误处理可能如下所示:

 error: function (xhr, ajaxOptions, thrownError) {
    window.location = 'http://example.com';
  }

【讨论】:

    【解决方案2】:

    这应该让你开始。

    $.ajax({
        url: "http://www.example.com"
    }).done(function() {
        alert('done')
    }).error(function() {
        alert('error')
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 2013-02-06
      相关资源
      最近更新 更多