【问题标题】:Get url status using jquery使用 jquery 获取 url 状态
【发布时间】:2012-04-20 10:46:53
【问题描述】:

我正在尝试获取 403 禁止错误的状态,我可以使用 php curl 来完成,但需要使用 jquery 来完成。

这是我的代码。

   $.ajax({

    url: 'https://s3.amazonaws.com/isdukg/01-jaimeson-complete_2_step_mix-pulse.mp3',
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    statusCode: {
      403: function() {
      alert("Forbidden");
      // or do something useful
      }
    }
});

$.ajax({

    url: 'https://s3-eu-west-1.amazonaws.com/fkcreate/Angles%2C%20Circles%20and%20Triangles..%20Instrumental.mp3',
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    statusCode: {
      403: function() {
      alert("Forbidden");
      // or do something useful
      }
    }
});

其中一个 url 将产生 403 禁止错误,如果您打开控制台窗口并加载页面,您可以在此处看到 - http://scaredem.users36.interdns.co.uk/

我似乎无法抓住这个错误来做一些有用的事情。

【问题讨论】:

  • 直接在浏览器中打开 url 会生成一个带有错误消息的 xml 文档。
  • 我得到的唯一信息是“服务器不支持 RFC 5746,请参阅 CVE-2009-3555”。

标签: jquery url status


【解决方案1】:

使用 jQuery ajax,您可以使用 statusCode 属性将函数映射到 HTTP 响应状态代码。

$.ajax({
statusCode: {
  403: function() {
  alert("Forbidden");
  // or do something useful
  }
}
});

【讨论】:

  • 我认为这是正确的方法,但它不起作用我已经更新了代码
  • 也许响应码不是 403,至少在我测试你的 URL 时不是。顺便说一句,如果您正在获取 mp3 文件,为什么 dataType 声明为 jsonp?
  • 我尝试了许多不同的数据类型以使其从文档中正常工作,我可以为他们的音频文件找到正确的数据类型吗?
【解决方案2】:

可能是timeout 在这种情况下为您提供帮助。如果请求在 500 毫秒内超时,则没有响应(我也尝试获取响应状态,但没有运气)。

jsfiddle

            $.ajax({
                url: "https://s3-eu-west-1.amazonaws.com/fkcreate/Angles%2C%20Circles%20and%20Triangles..%20Instrumental.mp3",
                type: "get",
                dataType: 'jsonp',
                crossDomain : true,
                timeout : 500, // it is milliseconds
                complete : function(xhr, responseText, thrownError) {
                    if(xhr.status == "200") {
                       alert(responseText); // yes response came
                    }
                    else{
                       alert("No response"); // no response came
                    }
                }
           });​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多