How to get HTTP status from JavaScript

How to get the HTTP status code of another site with JavaScript?

+3
source share
4 answers

I am assuming JavaScript with jQuery, which simplifies AJAX queries like this.

$.ajax({
  url: 'url',
  type: 'GET',
  complete: function(transport) {
      doing whatever..
  }
});
+1
source

Try the following javascript code snippet:

function getReq() {
    var req = false;
    if(window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
    } else if(window.ActiveXObject) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            req = false;
        }
    }
    if (! req) {
        alert("Your browser does not support XMLHttpRequest.");
    }
    return req;
}

    var req = getReq();

        try {
        req.open("GET", 'http://www.example.com', false);
        req.send("");
    } catch (e) {
        success = false;
        error_msg = "Error: " + e;
    }

alert(req.status);
+7
source

XMLHTTPRequest HTTP. , HEAD URL-. XMLHTTPRequest - xhr

 xhr.open("HEAD", <url>,true);
 xhr.onreadystatechange=function() {
     alert("HTTP Status Code:"+xhr.status)
 }
 xhr.send(null);

. .

+4

AJAX - .

You will need to configure the proxy service on your server and then make ajax calls with the address you want to check. From your proxy server you can use cURL or which tool you want to check the status code and return it to the client.

+2
source

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


All Articles