Javascript Node/Express-res.status().send()只发送状态,不发送对象
我后端的路由有问题,Javascript Node/Express-res.status().send()只发送状态,不发送对象,javascript,node.js,express,fetch,Javascript,Node.js,Express,Fetch,我后端的路由有问题,res.status().send()将只向客户端发送状态代码,但不会向客户端发送位于send()内部的对象 这是我的代码(为简洁起见,编辑了所有代码,但问题是): 当我从客户端发出fetch请求时,响应仅从服务器返回状态代码,但响应中没有服务器上send()方法中的对象。只是随口吐痰,我向它扔了res.status(200).json(object),以json的形式发送对象,但没有用 这是我从客户端发出的“获取”请求: fetch("http://localhost:
res.status().send()
将只向客户端发送状态代码,但不会向客户端发送位于send()
内部的对象
这是我的代码(为简洁起见,编辑了所有代码,但问题是):
当我从客户端发出fetch请求
时,响应仅从服务器返回状态代码
,但响应中没有服务器上send()
方法中的对象。只是随口吐痰,我向它扔了res.status(200).json(object)
,以json
的形式发送对象,但没有用
这是我从客户端发出的“获取”请求:
fetch("http://localhost:3000/users/accounts/", {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(userData)
}).then(response => console.log(response));
}
为了显示我得到的响应,我特意将一些表单数据从客户端发布到将抛出403错误的路由,这是我在浏览器控制台中得到的响应:
Response {type: "basic", url: "http://localhost:3000/users/accounts/", redirected: false, status: 403, ok: false, …}
因此,我能够成功地将状态从路由发送回客户端,但我始终无法理解为什么
send()
不将对象与之一起发送 从fetch()
返回的响应的主体是ReadableStream
。你需要处理它,把它变成有用的东西。通常,您会调用response.json()
将其解析为json对象:
fetch("http://localhost:3000/users/accounts/", {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(userData)
})
.then(response => response.json())
.then(response => console.log(response));
}
轰,就是这样。谢谢你的快速和有见地的回应。繁荣是我最喜欢的反应。
fetch("http://localhost:3000/users/accounts/", {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(userData)
})
.then(response => response.json())
.then(response => console.log(response));
}