我正在开发一个单位转换 API,我在使用 fetch() 时遇到了很多问题。我的后端 API 正在工作,例如,当我调用
curl "http://localhost:8000/unitconv/convert?to=lb&from=kg&value=10"
我得到了正确的回应:
{"units": "lb", "value": 22.04623}
。然而,当我用 fetch() 做同样的事情时,这就是我得到的:
我已经尝试了 localhost 和 127.0.0.1,但我仍然无法通过这个“未定义”。这里有什么问题吗?
答案 0 :(得分:1)
您在第一个 then 中使用大括号 then 没有返回,因此第二个 then 没有任何内容。应该是
.then((response) => {return response.json()}).then(() => ...
答案 1 :(得分:1)
你可以像这样简单地省略大括号:
fetch(url).then(response => response.json()).then(() =>