Express res.jsonp() Method

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to return JSONP responses with res.jsonp().
  • How callback wrapping works using query parameters.
  • How to customize the callback parameter name.
  • When to prefer CORS + JSON over JSONP.

Syntax

javascript
res.jsonp(body)
res.status(200).jsonp(body)
1

Return JSONP data for legacy client

javascript
app.get('/api/legacy-user', function (req, res) {
  res.jsonp({ id: 1, name: 'Mari' });
});
2

Customize callback query key

javascript
app.set('jsonp callback name', 'cb');

app.get('/api/legacy-stats', function (req, res) {
  res.jsonp({ ok: true, visits: 1200 });
});
3

Send error as JSONP with status

javascript
app.get('/api/legacy-orders/:id', function (req, res) {
  if (!/^\d+$/.test(req.params.id)) {
    return res.status(400).jsonp({ error: 'Invalid id' });
  }
  res.jsonp({ id: Number(req.params.id), status: 'queued' });
});

⚠️ Common pitfalls

  • Use JSONP only when required for legacy clients; prefer standard JSON APIs with CORS for modern apps.
  • Do not send sensitive data over JSONP endpoints.
  • Keep callback behavior predictable and avoid mixing JSON and JSONP semantics unintentionally.

❓ FAQ

It sends JSON data wrapped in a JavaScript callback function for JSONP-style responses.
res.json() returns plain JSON, while res.jsonp() may wrap the payload in a callback based on query parameter.
Mostly for legacy clients that need JSONP. Modern APIs usually use CORS with res.json().
Yes, Express supports changing it via app settings such as app.set('jsonp callback name', 'cb').
JSONP has limitations and risks; prefer CORS-based JSON APIs when possible.
Did you know?

res.jsonp() sends JSON wrapped in a callback function, enabling legacy cross-domain script-based consumption.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful