javascript - 获取“未处理的拒绝(SyntaxError):JSON 输入的意外结束”
问题描述
这是我的代码:
const userIds: string[] = [
// Squall
'226618912320520192',
// Tofu
'249855890381996032',
// Alex
'343201768668266496',
// Jeremy
'754681236236140666',
// Maddo
'711211838305599538',
// Arden
'375573674306306050',
// Neo
'718874307316678698',
// Mytho
'480092510505402380',
// Yuun
'630427600220717067'
];
const fetchData = async() => {
let users: User[] = [];
for (const id in userIds) {
const response = await fetch('https://api.codetabs.com/v1/proxy/?quest=http://hamsterland.herokuapp.com/api/users?id=' + id);
const user: User = await response.json();
users.push(user);
}
}
我得到错误Unhandled Rejection (SyntaxError): Unexpected end of JSON input
。
如果我分别使用每个 Id 的 API,都返回有效的 JSON。for
但是,它在循环中不起作用。
解决方案
如果你只是这样做
for (const id in userIds) {
console.log(id);
}
你会看到问题。在这个循环中,id
是键,而不是值。您可能会得到404
s 作为回报。
使用for... of
循环而不是for... in
.
推荐阅读
- node.js - AWS file upload with nodejs api not working
- python-3.x - Json file data in dictionary format
- javascript - XMLHttpRequest 加载 html 文件
- python - Compute the (Moore-Penrose) pseudo-inverse of a matrix in Python without NumPy
- gitlab - 如何在 Gitlab Wiki (Markdown) 中突出显示 JSON 块?
- python - Tkinter 从 os.system 命令返回 GUI 的结果
- swift - 如何在 Swift 中为工作周(周一至周五)设置 UNCalendarNotificationTrigger?
- ruby-on-rails - 如何检查通过 Rails 中的模型的对象?
- apache2 - 如何使用 proxy_ajp 将 HTTP 方法从 apache2 转发到 tomcat
- razor-pages - 尝试限制静态文件夹时,Razor 页面策略不起作用