Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript和ajax读取json对象由于CORS策略,我遇到了错误 文件 函数ondata(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var javaobj=JSON.parse(xhttp.response); document.getElementById('mass').innerHTML=javaobj.name; } }; open(“GET”,“student.json”,true); xhttp.send(); }_Javascript_Json_Ajax - Fatal编程技术网

使用javascript和ajax读取json对象由于CORS策略,我遇到了错误 文件 函数ondata(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var javaobj=JSON.parse(xhttp.response); document.getElementById('mass').innerHTML=javaobj.name; } }; open(“GET”,“student.json”,true); xhttp.send(); }

使用javascript和ajax读取json对象由于CORS策略,我遇到了错误 文件 函数ondata(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var javaobj=JSON.parse(xhttp.response); document.getElementById('mass').innerHTML=javaobj.name; } }; open(“GET”,“student.json”,true); xhttp.send(); },javascript,json,ajax,Javascript,Json,Ajax,我弄错了 6hp.html:21在'file:///C:/Users/Admin/Desktop/ind/ip-来自源“null”的simple/student.json已被CORS策略阻止:跨源请求仅支持协议方案:http、数据、chrome、chrome扩展名、https . 尝试将json文件移动到与html文件相同的目录 我可能错了,但浏览器将您可以从脚本访问的文件限制为同一目录中的文件 是否有本地(开发)服务器正在运行?您的浏览器无法直接从文件系统请求文件。我通过tomcat服务器运行

我弄错了
6hp.html:21在'file:///C:/Users/Admin/Desktop/ind/ip-来自源“null”的simple/student.json已被CORS策略阻止:跨源请求仅支持协议方案:http、数据、chrome、chrome扩展名、https . 尝试将json文件移动到与html文件相同的目录


我可能错了,但浏览器将您可以从脚本访问的文件限制为同一目录中的文件

是否有本地(开发)服务器正在运行?您的浏览器无法直接从文件系统请求文件。我通过tomcat服务器运行Ajax代码来清除错误
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
       function ondata(){
         var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function(){
          if(this.readyState==4 && this.status==200){

            var javaobj = JSON.parse(xhttp.response);
            document.getElementById('mass').innerHTML=javaobj.name;
          }
         };
         xhttp.open("GET","student.json",true); 
         xhttp.send();
       }


    </script>
    <input type="button"name="name" onclick="ondata()" value="Click"/>
    <div id="mass"></div>
</body>
</html>