How to get remote URL status code using Javascript / Ajax but NOT using jQuery?

I am trying to check if url is available or not using javascript, not using jQuery or the way to load the image from this url. It would be great if I could get the status code of this URL, because all I care about is a status code of 200.

I tried XMLHttpRequest, but I found out that it does not support cross-calls.

Please, help.

Thanks.

0
source share
3 answers

Create the server side of the script using the server language , let this script return a status code. This "proxy" script will ask you for a URL so that cross domain requests are not required.

See a sample Python script response code here: What is the best way to get the HTTP response code from a URL?

Hope this helps, -Sunjay03

+1
source

It is not possible to get the status code directly in JavaScript without XMLHttpRequest. As already mentioned, this is usually only a single-user domain, although there are two possible approaches to the cross-domain approach (not requiring a proxy server), which I know of:

On the other hand, if all that cares is an image that can be loaded (it may not be quite 200!), This can be found using the Image element and the onload , if the onload does not fire for a certain period of time , we can conclude that a timeout or failure was requested (4xx / 5xx). However, this will return a false positive result in the case of 302, etc.

Just make sure that the browser is really trying to download the image: some of the smarter ones will not download hidden images, or, as I already heard.

Happy coding.

+2
source

I am not sure if this is a cross browser:

 <script src="http://www.google.com" onerror="alert('error')"></script> <!-- Is Fine --> <script src="http://www.notrealnotrealzz.com" onerror="alert('error')"></script> <!-- Alerts Error --> 

And this is obviously a hack, but hopefully it helps.

More dynamic version:

 var el = document.createElement('script'); el.onerror = errorFunction; el.src = url; document.body.appendChild(el); 
+2
source

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


All Articles