has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested

问题描述:
学习菜鸟教程 异步编程 一小节的ajax编程部分,通过ajax请求不到数据,出现的详细错误如下图:
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
使用环境:Google浏览器

源代码如下:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>菜鸟教程(runoob.com)</title> 
</head>
<body>
<p id="demo1">
    时间
</p>
<p id="demo">我的第一个段落。</p>
 <img  id="img" src="http://image.biaobaiju.com/uploads/20190705/21/1562334093-dvzusNQBGr.jpg" alt="图片">
<script>
        var xhr = new XMLHttpRequest();
        xhr.onload = function()
        {
            document.getElementById("demo").innerHTML=xhr.responseText;
        }
        xhr.onerror = function()
        {
            document.getElementById("demo").innerHTML = "请求出错";
        }
        xhr.open("GET", "https://www.runoob.com/try/ajax/ajax_info.txt", true);
        xhr.send();
</script>
</body>
</html>

解决方案:
需要用Google浏览下载,可能需要*
下载地址https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=zh-CN
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
然后打开它
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested

打开它,让它图标变成彩色
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
再尝试就可以通过ajax获得数据了

has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested

上一篇:Android——长按弹出菜单、获取当前所按的item的信息


下一篇:docker的学习