我怎样才能得到我的url请求的Json?
我想做一个URL请求,其结果将是一个JSON
后,我已经在浏览器中尝试过。 我想把整个JSON响应放在一个var或者const中,以便以后进一步处理。
我到目前为止所尝试的:
app.use(express.bodyParser()); app.post(MY_URL, function(request, response){ console.log(request.body); console.log(request.body; });
然后 :
app.use(bodyParser.urlencoded({ extended: true })); app.post(MY_URL, function (req, res) { console.log(req.body) });
这两者都还没有成功,并在node.js
是一个初学者没有帮助。
编辑:为了澄清我的问题:
my_url = https://only_an_example
在浏览器中键入的URL将在该页面中显示Json,如下所示:
{ "query": "testing", "topScoringIntent": { "intent": "Calendar.Add", "score": 0.987683 }, "intents": [ { "intent": "Calendar.Add", "score": 0.987683 }, { "intent": "None", "score": 0.0250480156 }}
我想要的是得到这个JSON响应并使用node.js打印它。
如果您正在尝试获取请求的正文,只需访问:
req.body;
如果你想发送一个JSON对象作为响应,你可以这样做:
var objectToResponde = {"key1": "value1", "key2": "value"}; res.send(objectToResponde);
在进一步了解OP的问题之后(通过下面的注释),您可以将要发送回客户端的对象转换为JSON,如下所示:
app.post('some/url', (req, res) => { const myObject = {a: 1, b:2}; res.json(myObject); });
这是一个JSON响应与适当的响应头集。