View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

res.jsonp()

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.

Usage

return res.jsonp([statusCode, ] data);

Example

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" }) ``` -->

Notes

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

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book