res.jsonp()
Send a JSON or JSONP response.
Identical to res.json()
except that, if a request parameter named "callback" was provided in the query string, then Sails will send the response data as JSONP instead of JSON. The value of the "callback" request parameter will be used as the name of the JSONP function call wrapper in the response.
return res.jsonp(data);
In an action:
return res.jsonp([
{
name: 'Thelma',
id: 1
}, {
name: 'Leonardo'
id: 2
}
]);
Given ?callback=gotStuff
, the code above would send back a response body like:
gotStuff([{name: 'Thelma', id: 1}, {name: 'Louise', id: 2}])
- Don't forget that this method's name is all lowercase.
- If no "callback" request parameter was provided, this method works exactly like
res.json()
.- This method is terminal, meaning that 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).