1

I am making a get request to google.com to check if the site is responding & the response time. I am using JavaScript to achieve it but I am being blocked by the Cross-origin request policy.

I do not want to download any data from the external site (google.com in this example) but only want to check the status & response time.

Any thoughts, how can I achieve it? I am new to JavaScript...

let start = Date.now();
let promise = fetch("https://www.google.com");
let end = Date.now();
alert( `The request took ${end - start} ms` );
j3ff
  • 5,719
  • 8
  • 38
  • 51

1 Answers1

2

Extend the fetch API and add some timing:

window.fetch = (function(fetch) {
  return function(fn, t) {
    const begin = Date.now();
    return fetch.apply(this, arguments).then(function(response) {
      response.timing = Date.now() - begin;
      return response;
    });
  };
})(window.fetch);

fetch('https://httpbin.org/json')
  .then(function(response) {
    console.log(response.timing + 'ms')
    return response.json();
  }).then(function(jsonData) {
    console.log(jsonData);
  }).catch(function(err) {
    console.log("Opps, Something went wrong!", err);
  })
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106