JSONP (JSON with Padding)
A legacy technique for cross-origin data loading by wrapping JSON in callbacks. Deprecated due to security risks including data theft and XSS.
JSONP (JSON with Padding) is a legacy technique for bypassing Same-Origin Policy to load cross-origin data by wrapping JSON in a callback function. While it enabled cross-domain data sharing before CORS, it introduces security risks and is largely deprecated.
How JSONP Works
// Client requests with callback parameter
<script src="https://api.example.com/data?callback=handleData"></script>
// Server returns executable JavaScript
handleData({"name": "John", "email": "john@example.com"});
// Client defines callback to receive data
function handleData(data) {
console.log(data.name);
}
Why JSONP is Dangerous
- Executes arbitrary code from third parties
- No way to verify response integrity
- Credentials sent automatically (cookies)
- Enables XSSI attacks
- Callback injection can lead to XSS
Modern Alternative
Use CORS with proper Access-Control-Allow-Origin headers instead of JSONP for cross-origin data access.