18910140161

JavaScript-获取JSON不在站点中工作,但在本地主机中工作-堆栈溢出

顺晟科技

2022-10-18 13:37:07

157

JSON抓取以前在站点中工作过,例如,在这里,我使用了相同的流程-https://n-ce.github.io/sea-arch

这是剧本

//FETCH
function content(a){
fetch(a+'.json').then(function (response) {
      return response.json();
    }).then(function (data) {
      appendData(data);
    }).catch(function (err) {
      console.log('error: ' + err);
    });
}

//Loading the objects
var root = document.getElementById('root');
function appendData(data) {
  for (var i = 0; i < data.length; i++) {
    var p = document.createElement("p");
    p.innerHTML = data[i].Name;
    root.appendChild(p);
  } 
}


// Remove Function

function remove(){
  while (root.hasChildNodes()) {
    root.removeChild(root.firstChild);
  }
}

// Click decision making

var count = couns = 0;

function Sites() {
  if(count%2==0){
    content('Sites');
    count++;
  }
  else{
    count--;
    remove();
  }
}
function Animals() {
  if (couns % 2 == 0) {
    content('Animals');
    couns++;
  }
  else {
    couns--;
    remove();
  }
}

这是HTML

<代码><;!文档类型HTML>;<;HTML Lang=";英文";>;<;头部>;<;元字符集=";UTF-8";>;<;元名称=";视口";内容=";宽度=设备宽度,初始比例=1.0";>;<;标题>;JSON SD<;/Title>;<;link rel=";样式表";href=";style.CSS";/>;<;/标题>;<;正文加载=";内容(' onload ')";>;<;范围>;<;按钮onclick=";动物()";>;加载动物<;/按钮>;<;按钮onclick=";站点()";>;加载站点<;/按钮>;<;/span>;<;DIV ID=";根目录";>;<;/DIV>;<;脚本SRC=";script.JS";>;<;/script>;<;/正文>;<;/HTML>;

我无法对此进行调试,因为控制台不会抛出任何错误,因为它在本地主机中工作正常。我对Fetch API相当陌生,但是我想可能是因为传输JSON文件的时间太长了?有没有人能强调一下问题是什么?


顺晟科技:

下面是GitHub页面的工作方式,感谢@ProgrammarRaj的突出显示剧本


<!DOCTYPE html>
<html lang="en" >
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON SD</title>
    <link rel="stylesheet" href="style.css"/>
  </head>
  <body onload="content('Onload')">
    <span>
      <button onclick="Animals()">Load Animals</button>
      <button onclick="Sites()">Load Sites</button>
    </span>
    <div id="root"></div>
    <script src="script.js"></script>
  </body>
</html>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航