我正在尝试对我的服务器执行简单的GET请求。URL是正确的,它使用python函数给出json。但是我无法在javascript中获得相同的数据。此函数有问题。 async function get_status(){ socket.send("Status!"); URL = http://127.0.0.1:8000/api/status/'; response = await fetch(URL, { method: "GET", headers: { "Accept": "application/json" } }) socket.send(response.json()); console.log(response.json()); if (response.ok) { current = document.getElementById("status"); current.value= response.json()["status"]; } };
1 回答

当年话下
TA贡献1890条经验 获得超9个赞
请尝试:
const response = await fetch(URL);
if (response.ok) {
const json = await response.json();
console.log(json);
socket.send(json);
current = document.getElementById("status");
current.value= response.json()["status"];
} else {
console.log('request failed', response);
}
解释
fetch(URL)返回,响应实现 Body 接口,这意味着返回另一个你应该“等待”的承诺。Promise<Response>response.json()
此外,只有在返回响应时,您才能获取,这意味着此代码应在包装中执行。response.json()okif (response.ok) { ...
有关详细信息,请参阅: https://www.npmjs.com/package/node-fetch#fetchurl-options
添加回答
举报
0/150
提交
取消