0

may i know what's wrong with my code? it keep return me 0 no matter the url links is available or not.

<Script>
  function doSomethingIfFound(url, status) {
    alert(url + " was found, status " + status);
  }

  function doSomethingIfNotFound(url, status) {
    alert(url + " was not found, status " + status);
  }

  function test(url) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        doSomethingIfFound(url, xhttp.status);
      } else if (xhttp.readyState == 4 && xhttp.status != 200) {
        doSomethingIfNotFound(url, xhttp.status);
      }
    };
    xhttp.open("GET", url, true);
    xhttp.send();
  }


  test("https://test.e-cover.com.my/");
  test("https://www.e-cover.com.my/");
  test("http://www.mypolicy.com.my/mypolicy/login.jsp/");
  test("https://itdidnotexistwhenitrieitiswear.museum/");
</script>
user3835327
  • 1,194
  • 1
  • 14
  • 44

1 Answers1

0

For the first two urls:

https://www.e-cover.com.my
https://test.e-cover.com.my

I'm getting this error in firebug: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Here is a post that explains CORS

Apparently many websites require this in order for a script that originates from another website to do http requests on them. There are websites that are more permissive such as this one:

test("http://www.omdbapi.com/?i=tt1285016");

which works fine.

The other two urls:

https://itdidnotexistwhenitrieitiswear.museum
http://www.mypolicy.com.my/mypolicy/login.jsp

are simply failing to open with normal status 200

Community
  • 1
  • 1