Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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 使用typeScript获取本地JSON文件。我在静态页面中遇到了一个跨原点错误_Javascript_Json_Typescript - Fatal编程技术网

Javascript 使用typeScript获取本地JSON文件。我在静态页面中遇到了一个跨原点错误

Javascript 使用typeScript获取本地JSON文件。我在静态页面中遇到了一个跨原点错误,javascript,json,typescript,Javascript,Json,Typescript,我正在使用typeScript获取本地JSON文件。我在静态页面中遇到一个跨原点错误。 这是我的密码: window.onload = function() { // // all of your code goes in here // // it runs after the DOM is built function loadJSON(path, success, error) { var xhr = new XMLHttpRequest(); xhr.onreadysta

我正在使用typeScript获取本地JSON文件。我在静态页面中遇到一个跨原点错误。 这是我的密码:

window.onload = function() {
//   // all of your code goes in here
//   // it runs after the DOM is built
function loadJSON(path, success, error) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      if (xhr.status === 200) {
        if (success) success(JSON.parse(xhr.responseText));
      } else {
        if (error) error(xhr);
      }
    }
  };
  xhr.open("GET", path, true);
  xhr.send();
}

loadJSON(
  "../dataBase/electrical.json",
  function(data) {
    console.log(data);

    function getSgItem() {
      let sg = "";

      data.forEach(function(item) {
        sg += `
                <div class="data">
                    <div class="image">
                    <img src="../images/electrical/scherer01.png" alt="" />
                    </div>
                    <div class="dataContent">
                    <h2>${item.sgice}</h2>
                    <h1>${item.name}</h1>
                    <div class="button">
                        <div class="buttonInfo">
                        <h3>SHIPS#</h3>
                        <h3>${item.ships}</h3>
                        </div>
                        <button>
                        <a href=${item.link}>CLICK HERE</a>
                        </button>
                    </div>
                    </div>
                </div>
            `;
      });
      document.getElementById("sg").innerHTML = sg;
    }

    getSgItem();
  },
  function(xhr) {
    console.error(xhr);
  }
);
}
window.onload=function(){
////所有代码都放在这里
////它在生成DOM后运行
函数loadJSON(路径、成功、错误){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==XMLHttpRequest.DONE){
如果(xhr.status==200){
if(success)success(JSON.parse(xhr.responseText));
}否则{
if(error)error(xhr);
}
}
};
xhr.open(“GET”,path,true);
xhr.send();
}
loadJSON(
“./dataBase/electrical.json”,
功能(数据){
控制台日志(数据);
函数getSgItem(){
设sg=“”;
data.forEach(功能(项){
sg+=`
${item.sgice}
${item.name}
船舶#
${item.ships}
`;
});
document.getElementById(“sg”).innerHTML=sg;
}
getSgItem();
},
函数(xhr){
控制台错误(xhr);
}
);
}
我需要将其作为公司内部服务器的静态页面运行。有办法解决这个问题吗

使用vs代码,当我右键单击并使用live server打开时,它可以正常工作


非常感谢。

您遇到了什么错误?这不是因为typescript,而是因为CORS I imagineMy错误:jquery-3.4.1.min.js:2在'file:///C:/Users/Andre/Documents/Programing/powerIE/dist/dataBase/electrical.jsonCORS策略已阻止“来自源”的“null”:跨源请求仅支持协议方案:http、数据、chrome扩展、edge、,https。