Edit Page

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.

Usage

#
return res.jsonp(data);

Example

#

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}])

Notes

#
  • 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).

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference