json - 如何使用 express 从 403 发回 json

标签 json node.js express

我想用 express 做类似下面的事情。

let error = {errorCode:"1234"}
res.sendStatus(403, {error: error});

在我的前端 javascript 中,我想捕获错误并像这样检查它

getData(mydata).then((data)=> {
     console.log(data);
).catch((error)=> {
     console.log(error.errorCode);
});

无论出于何种原因,这都没有将 json 发回我的 catch 方法,我想知道为什么以及如何在我发回 403 时发送 json。

最佳答案

使用这个语法

res.status(403).send({errorCode:"1234"});
// or
res.status(403);
res.send({errorCode:"1234"});

关于json - 如何使用 express 从 403 发回 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879060/

相关文章:

json - Swift 将 ObjectMapper 与单例一起使用

node.js - Express JS 在请求中使用异步函数

javascript - 视频流不适用于 iOS/Safari

node.js - 删除 express 中的 Etag header

Node.js: express 不工作?

php - Youtube API PHP json解码

c++ - HasMember 期间的 Rapidjson 断言

java - 将 GWT 客户端对象序列化为字符串并在服务器上反序列化的方法?

node.js - 如何从 NodeJS Lambda 函数中的 axios.get 等异步函数获取返回值?

node.js - 如何从 express 和 node 中的 html 表单发送放置请求