Use javascript to check http status codes

I want to display profile photos from gravatar only for those users who have a set of images. Executing this server part means executing about 100 HEAD requests in gravatar to check 404 codes and the corresponding output of img tags for each request.

So, I want to implement a javascript function where I can simply output 100 URLs for which javascript can check the http status codes and output the corresponding image tags dynamically. Is it possible? How?

+3
source share
1 answer

The key word that you are missing is a โ€œstatus codeโ€ (what we collectively call all HTTP response codes 200, 404, 500, etc.). I assume you are using jQuery, in which case all the documentation necessary for AJAX to work is at http://api.jquery.com/jQuery.ajax/

Here is a simple request example that displays a warning, but only if the 404 status code is returned (almost literally lifted 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> 
+5
source

Source: https://habr.com/ru/post/1241876/


All Articles