0

enter image description here I have a requirement in my project ,where I need to the check the URL is working or not in Javascript. If that URL is working in need to get 200 response code

I tried this code,with this code I getting error. I am not getting 200 response

function checkLink(url) { 
  var http = new XMLHttpRequest();
  
     
      http.open('GET',url, true);
       http.setRequestHeader("Access-Control-Allow-Origin","https://vivr.pgf-life.co.jp/1/top");
      http.setRequestHeader("Access-Control-Allow-Methods", "GET,POST,PUT");
      http.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      http.send();
      console.log("status=",http.status);
      if(http.status===404)
      return false;
      else
      return true; 
  }
var a=checkLink("https://www.google.com/")
console.log(a);
KLINTON J
  • 1
  • 1
  • Well, you’re not using a `load` event listener on `http`, so there’s no way to listen for completion. Please familiarize yourself with [asynchronous execution](/q/23667086/4642212). Use [the Fetch API](//developer.mozilla.org/en/docs/Web/API/Fetch_API/Using_Fetch) instead. – Sebastian Simon Dec 10 '22 at 13:02
  • Thanks @SebastianSimon . can u please provide a sample code for checking the http://www.google.com using the Fetch API and I need to get 200 ok response , and I am new to this concepts please help me. – KLINTON J Dec 10 '22 at 13:18

0 Answers0