如何将数组字符串转换为数组

柯蒂斯·克伦西尔

我正在从 PHP 文件中获取一个数组,当我在控制台上记录该数组时,它显示为:[{"name":"zdfad","email":"XXX","phone":"XXX","id":"0","level":"1"}]作为字符串而不是作为数组。

我怎样才能解决这个问题?这是代码:

fetch("./php/getuser.php", { method: "GET" })
.then((res) => res.text())
.then((data) => {
    console.log(data);
  
});
sam-sjs

现在您正在使用text()which 要求 fetch 读取响应并将其解析为字符串。请参阅:https : //developer.mozilla.org/en-US/docs/Web/API/Response/text

你想要的是,以取代.text().json()参见:https://developer.mozilla.org/en-US/docs/Web/API/Response/json

这应该有效:

fetch("./php/getuser.php", { method: "GET" })
.then((res) => res.json())
.then((data) => {
    console.log(data);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章