Send a JSON or JSONP response.
Identical to res.json()
, except if a "callback" parameter exists, a JSONP response will be sent instead, using the value of the "callback" parameter as the name of the function wrapper.
return res.jsonp([statusCode, ] data);
return res.jsonp({
users: [{
name: 'Thelma',
id: 1
}, {
name: 'Leonardo'
id: 2
}]
});
// ?callback=foo res.jsonp({ user: 'tobi' }) // foo({ "user": "tobi" })
app.set('jsonp callback name', 'cb');
// ?cb=foo res.jsonp(500, { error: 'message' }) // foo({ "error": "message" }) ``` -->
- Don't forget this method's name is all lowercase.
- This method is terminal, meaning it is generally the last line of code your app should run for a given request (hence the advisory usage of
return
throughout these docs).