Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

res.jsonp

res.jsonp() sends a JSON response with JSONP callback support. It accepts a JSON-compatible body; set status separately with res.status().jsonp(body).

How it works

Call res.jsonp with any JSON-compatible value to send it as a response with JSONP support. Express uses the callback query parameter and wraps the serialized value with that callback; configure the default callback parameter name through the application setting. Set HTTP status separately with res.status().

Syntax

res.jsonp(body)
res.status(status).jsonp(body)

Parameters

NameTypeDescription
bodyany | undefinedThe JSON-compatible value to serialize and send as the JSONP response; it may be omitted.
statusNumber | undefinedDeprecated positional HTTP status code; use res.status() instead.

Returns

Response

Key points

  • Use res.jsonp when a client needs a JSON payload wrapped in a JavaScript callback invocation.
  • The body is the payload Express serializes and wraps; it may be omitted or undefined.
  • The payload is not limited to objects: primitive JSON values and null are valid too.
  • Serialization follows the same general response behavior as res.json, with callback wrapping added.
  • JSONP behavior is specifically enabled by choosing this response method instead of ordinary JSON output.
  • Configure the application-level jsonp callback name setting when the default callback parameter name should differ.

Common mistakes

  • Do not pass the status as a second positional argument; use res.status(status).jsonp(body).
  • The older status-first overload is also deprecated; chain res.status() before res.jsonp().
  • Clients normally provide the callback through the callback query parameter; change the application setting if another default name is required.

Example: Send a JSONP response

const express = require('express');
const app = express();

app.get('/data', (req, res) => {
  res.jsonp({ user: 'tobi' });
});

app.listen(3000);

Start the server and request /data?callback=handleData to receive the object wrapped for JSONP.

When to use it

Serve JSONP to script-based clients

A legacy browser client requests cross-origin data through a script callback.

Wrap API data for callback consumers

An endpoint must return an object wrapped in a named callback function.

Send JSONP with an explicit status

A response needs a non-default HTTP status while retaining JSONP formatting.

Interview questions

QWhat does res.jsonp do?

res.jsonp(body) sends a JSON response while opting into JSONP callback support. The response is wrapped using the requested callback, such as callback({...}).

Follow-up: How should a non-success status be sent? Call res.status(code).jsonp(body), rather than using a deprecated positional status overload.

QWhat can the body argument contain?

The body argument can be any JSON type, including null, objects, arrays, strings, numbers, or booleans. Express serializes that value and emits it with JSONP support.

Follow-up: What should replace res.jsonp(body, status)? Use res.status(status).jsonp(body).

QHow is the callback name selected?

The default JSONP callback name is callback. The application can override it with the jsonp callback name setting.

Follow-up: What related response setting can improve security when responses may be interpreted as HTML? json escape makes res.json and res.jsonp escape <, >, and & as Unicode sequences.

  • res.json()
  • res.status()
  • jsonp callback name
  • json escape

Source: Response - Express.js 4.x. Checked 2026-10-15.