gpt4 book ai didi

javascript - 成功插入mongo后node.js响应?

转载 作者:行者123 更新时间:2023-11-30 13:36:22 25 4
gpt4 key购买 nike

我正在做一个 ajax $.post 到一个插入到 mongodb 集合中的 node.js 路由

collection.insert(req.body.content, function () {
return req.body.content
});

我如何返回一些东西,以便我可以在客户端回调中对响应做一些事情?

例如在 php 中我可以回显。

最佳答案

您应该有权访问请求对象(在您的示例中为 req)和响应对象。您需要做的就是写入响应对象。

res.writeHead(200, {
'Content-type': 'text/plain' // or whatever content type you want
});
// you can use res.write also, but call end when you are done.
res.end('the text you want to send');

关于javascript - 成功插入mongo后node.js响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com