使用javascript检查http状态代码 [英] Use javascript to check http status codes

查看:89
本文介绍了使用javascript检查http状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅为那些拥有图片集的用户显示来自gravatar的个人资料图片。执行此服务器端意味着向gravatar执行大约100个HEAD请求,以检查404代码并为每个请求适当地输出 img 标记。

I want to display profile pictures from gravatar for only those users who have a picture set. Doing this server side means doing around 100 HEAD requests to gravatar for checking 404 codes and appropriately outputting img tags for each request.

所以,我想实现一个javascript函数,我可以输出100个url,javascript可以检查http状态代码并动态输出相应的图像标签。这甚至可能吗?如何?

So, I want to implement a javascript function where I can just output 100 urls for which javascript can check the http status codes and output the appropriate image tags dynamically. Is that even possible? How?

推荐答案

您缺少的关键字是状态代码(这就是我们共同调用的所有HTTP响应代码) 200,404,500等)。我假设您正在使用jQuery,在这种情况下,执行AJAX所需的所有文档都在 http://api.jquery.com/jQuery.ajax/

The keyword you're missing is "status code" (that's what we collectively call all the HTTP response codes of 200, 404, 500, etc). I'm going to assume you're using jQuery, in which case, all the documentation you need for doing AJAX is at http://api.jquery.com/jQuery.ajax/

以下是显示警报的请求的简单示例,但仅限于返回404状态代码(几乎逐字逐句解除上面的链接):

Here's a simple example for a request that displays an alert, but only if a 404 status code is returned (lifted almost verbatim the link above):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
   $(function() {
      var url = "some_url";
      $.ajax(url,
      {
         statusCode: {
         404: function() {
            alert('page not found');
         }
      }
   });   
});
</script>

这篇关于使用javascript检查http状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆