【问题标题】:404 detection in JavaScriptJavaScript 中的 404 检测
【发布时间】:2011-05-30 03:30:00
【问题描述】:

在我的 JavaScript 中,我试图重定向到第三方页面。它可以在新窗口或框架内打开页面,具体取决于用户设置。像这样的:

if (newWindow)
{
   window.open(url, targer);
}
else
{
   theFrame = url;
}

我想做的是在第三方网站关闭或页面不可用的情况下显示我的自定义页面。基本上是在404错误的情况下。

解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: javascript asp.net http-status-code-404


    【解决方案1】:

    由于same origin policy,您无法检测到有关另一个窗口或框架的任何内容——包括它是否实际加载——如果它位于不同的域名上。

    【讨论】:

      【解决方案2】:

      另一种想法... 您可以使用服务器端语言检查目标网址。

      对于 PHP:使用 Curl 可以获取 url 的 http 状态。

      http_code 就是你要找的 curl_getinfo ( $ch [, int $opt = 0 ] )

      http://www.php.net/manual/en/function.curl-getinfo.php

      【讨论】:

      • +1 用于解决方法,但我敢打赌 Vadim 想要一个纯 JavaScript 版本。
      • 没有任何 javascript 解决方案的可能性。事实上,如果有的话。这是一个危险的错误,可能是毁灭性的......
      • 与 ASP.Net 我认为它必须是这样的。这很容易。 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("domain.com/urlnotfound.html"); HttpWebResponse httpresponse = (HttpWebResponse)webrequest.GetResponse(); Response.Write(httpresponse.StatusCode); 评论框不好粘贴代码...试试这个网址msdn.microsoft.com/en-us/library/…
      【解决方案3】:

      按照@risyasin 的建议,我使用 ASP.NET 在服务器端解决了我的问题。

      protected void Page_Load(object sender, EventArgs e) {
          HttpWebResponse response;
          try
          {
              var request =
                  (HttpWebRequest)WebRequest.Create("http://www.someSite.com/camdsa");
              response = request.GetResponse() as HttpWebResponse;
          }
          catch (WebException webException)
          {
              Response.Redirect("ErrorPage.aspx?status="
      + webException.Status);
              return;
          } }
      

      您可以在link 阅读我的解决方案。

      【讨论】:

        猜你喜欢
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 2012-06-23
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        • 2016-03-06
        • 2014-01-15
        相关资源
        最近更新 更多