【问题标题】:"\" in JSON responseJSON 响应中的“\”
【发布时间】:2021-09-20 04:34:48
【问题描述】:

所以我的应用程序从我的数据库中获取数据,但它发送带有“\”的响应

快速回复

[
          {
                    "ID": "account-saassan",
                    "data": "{\"uuid\":\"saassan\",\"username\":\"sasasasasa\"}"
          }
] 

控制台输出:

[
  {
    ID: 'account-saassan',
    data: '{"uid":"saassan","username":"sasasasasa"}'
  }
]

代码:

const UserData = new db.table("userdata")

app.use((req, res, next) => {
    res.append('Access-Control-Allow-Origin', ['*']);
    res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.append('Access-Control-Allow-Headers', 'Content-Type');
    next();
});
app.get("/list", (request, response) => {
  
   
    let data = UserData.all()
    console.log(data)
    response.type('json');
    response.json(data)
// I've tried with response.send too. And without response.type
})


app.listen(8080)

为什么会在响应中添加“”,我该如何解决?

【问题讨论】:

    标签: node.js express quick.db


    【解决方案1】:

    尝试使用 JSON.stringify

    response.json(JSON.stringify(data))
    

    添加请添加:

    response.setHeader('Content-Type', 'application/json');
    

    【讨论】:

    • 同样的,加了“\”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多