[英]XMLHttpRequest status keep return 0
我可以知道我的代码有什么问题吗? 无论网址链接是否可用,它都会始终返回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>
对于前两个网址:
https://www.e-cover.com.my
https://test.e-cover.com.my
我在萤火虫中遇到此错误:跨域请求被阻止:同源策略禁止读取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打开
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.