【问题标题】:Disappearing element after parsing JSON解析JSON后消失的元素
【发布时间】:2023-03-10 12:32:01
【问题描述】:

我遇到了一个有趣的问题:我编写了一个 HTML/JS 项目,该项目通过 XMLHttpRequest 从 Scratch 网站获取信息。目标是让某人使用JSON.parse 输入他们的用户名并查看他们在 Scratch 上的个人资料中的描述。它似乎可以工作,但描述会短暂出现并消失。我已经扫描了我的代码,但仍然无法将描述保留在那里。我不会在 Stack Overflow 上发布代码(太多),但您可以在 https://getthatdesc--micahlt.repl.co/ 查看我的代码和输出。如果有人可以提供帮助,我将不胜感激!

【问题讨论】:

  • 欢迎来到 Stackoverflow。您的代码应发布在此处。该网站有充足的设施来格式化代码,甚至可以创建可执行示例作为您问题的一部分。
  • 我的第一个猜测是,您使用的表单默认在提交数据时刷新页面,在被调用的函数中尝试添加e.preventDefault() 以防止页面刷新跨度>
  • @Pointy 好的,抱歉。不知道这是规矩。以后会做的。
  • @KevinHernandez 可以,但不幸的是我没有用事件监听器编写代码。我使用了onsubmit 表单属性。
  • @micahlt 您的函数应该如下所示:function showInfo(){//code here 将其更改为 function showInfo(e){e.preventDefault(); //rest of code here}

标签: javascript html json xmlhttprequest repl.it


【解决方案1】:

您正在提交表单,默认行为是在提交表单时刷新页面

您的代码:

function showInfo() {
// set xhttp as an identifier of XMLHttpRequest
var xhttp = new XMLHttpRequest();
// every time the state of the request changes, evaluate
xhttp.onreadystatechange = function() {
  // These codes mean that the request is finished
  if (this.readyState == 4 && this.status == 200) {
    // Parse the JSON response
    var userParsed = JSON.parse(this.responseText);
    // log the response to the console
    console.log(userParsed);
    // get the description
    var description = userParsed.profile['bio'];
    // log description to the console
    console.log(description);
    // make the output visible to the user
    document.getElementById("info1").innerHTML = description;
  };
};
xhttp.open("GET", "https://cors-anywhere.herokuapp.com/api.scratch.mit.edu/users/" +document.getElementById('usrname').value, false);
xhttp.send();
}
/* fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json)) */

这需要改为:

function showInfo(e) {
  e.preventDefault();
  // set xhttp as an identifier of XMLHttpRequest
  var xhttp = new XMLHttpRequest();
  // every time the state of the request changes, evaluate
  xhttp.onreadystatechange = function() {
    // These codes mean that the request is finished
    if (this.readyState == 4 && this.status == 200) {
      // Parse the JSON response
      var userParsed = JSON.parse(this.responseText);
      // log the response to the console
      console.log(userParsed);
      // get the description
      var description = userParsed.profile["bio"];
      // log description to the console
      console.log(description);
      // make the output visible to the user
      document.getElementById("info1").innerHTML = description;
    }
  };
  xhttp.open(
    "GET",
    "https://cors-anywhere.herokuapp.com/api.scratch.mit.edu/users/" +
      document.getElementById("usrname").value,
    false
  );
  xhttp.send();
}
/* fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json)) */

【讨论】:

  • 我有点困惑......我需要一个参数来放入showInfo 函数吗?例如,showInfo(usrname).
  • 不,我为你写的应该可以。当函数被调用时,事件参数会自动传递给函数eevent 的缩写,所以:event.preventDefault() 继续阅读Events
  • 好的,谢谢!我对 JavaScript 有点陌生,感谢您的帮助。 ?
  • Np 伙伴,编码愉快!
猜你喜欢
  • 2014-10-31
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2017-07-14
  • 1970-01-01
相关资源
最近更新 更多