【问题标题】:XMLHttpRequest status keep return 0XMLHttpRequest 状态保持返回 0
【发布时间】:2016-04-24 18:04:23
【问题描述】:

我可以知道我的代码有什么问题吗?无论 url 链接是否可用,它都会返回 0。

<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>

【问题讨论】:

    标签: javascript html hyperlink xmlhttprequest


    【解决方案1】:

    对于前两个网址:

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

    我在 firebug 中收到此错误: 跨域请求被阻止:同源策略不允许读取 url 处的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。 这是一篇解释CORS

    的帖子

    显然,许多网站都需要这样做,以便源自另一个网站的脚本对它们执行 http 请求。 有些网站比较宽松,比如这个:

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

    效果很好。

    另外两个网址:

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

    只是无法以正常状态 200 打开

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多