reactjs - 解析失败 SyntaxError:输入意外结束
问题描述
我正在尝试从 API 检索响应,但是在使用之后
fetch("url").then(res=>res.json()),
在以下行中,我的代码中出现了意外的输入错误结束:
res => res.json(),
请找到下面的代码并帮助我准确定位我哪里出错了:-
static fetchCurrentServices(){
return fetch("http://localhost:8080/portal-backend/services", {
mode: "no-cors",
method: "GET" ,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
}
})
.then(
res => res.json()
).catch(function (ex) {
console.log('parsing failed', ex)
});
}
解决方案
您可以尝试下面的代码吗?我认为您错过了添加 {} 并添加了更多更改,请查看。
static fetchCurrentServices(){
return fetch("http://localhost:8080/portal-backend/services", {
mode: "no-cors",
method: "GET" ,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
}
}).then(res =>{
return new Promise((resolve) => {
if (res) {
res.json().then(json => resolve(json)).catch(() => resolve(null))
} else {
resolve(null)
}
})
}).catch(function (ex) {
console.log('parsing failed', ex)
});
}
推荐阅读
- amazon-web-services - *.tfvars 文件和在 terraform 中的 main.tf 中传递值之间的区别
- python - 机器人框架:无法在 Firefox 浏览器上加载配置文件
- c# - 使用请求/响应模式处理 ASP.NET Core Web API 异常
- java - 如果一个元素后面跟着相同的元素n次,如何检查二维数组
- javascript - 基于对象迭代动态获取数据库值
- java - 如何将 Map 转换为 javax.json.JsonObject?
- python - Tkinter Notebook 框架中的滚动条
- python - 从 3d 数据元素中删除异常值
- mysql - MySQL左连接不显示
- arrays - 如何提取 Pug 数组中每个第一项的值