---
title: "What is JSONP, and why is it used in web development?"  
description: "What is JSONP, and why is it used in web development?"  
author: "Utpal Vishwas"  
published: 2023-10-10  
updated: 2023-10-20  
canonical: https://www.mindstick.com/forum/160093/what-is-jsonp-and-why-is-it-used-in-web-development  
category: "json"  
tags: ["javascript", "json", "jsonp"]  
reading_time: 4 minutes  

---

# What is JSONP, and why is it used in web development?

What is [JSONP](https://www.mindstick.com/forum/159006/what-is-jsonp-and-why-was-it-created-please-explain), and why is it used in web development?

## Replies

### Reply by BigOhTech

JSONP (JSON with Padding) is a technique used in web development to overcome the same-origin policy limitations of web browsers. It allows you to make cross-domain requests for data from a different domain than the one serving your web page.

JSONP works by creating a script tag in your web page's HTML, which references a JavaScript file on the external domain you want to fetch data from. The server on that external domain wraps the data in a function call specified by the client. When the script is loaded, it executes the function, effectively passing the data to your web page.

JSONP is used when you need to fetch data from a different domain, typically for APIs or data sharing. It's a simple and widely supported method for cross-domain data retrieval, but it has security limitations and should only be used with trusted sources, as it can expose your website to potential security risks. In modern web development, alternatives like Cross-Origin Resource Sharing (CORS) are often preferred for more secure cross-domain data requests.

### Reply by Aryan Kumar

JSONP (JSON with Padding) is a technique used in web development to overcome the same-origin policy limitation of XMLHttpRequest, which restricts web pages from making requests to a different domain (origin) than the one the page came from. JSONP allows web developers to make cross-origin requests by dynamically adding script elements to a web page. Here's how JSONP works and why it's used:

## How JSONP Works:

1. A web page on one domain wants to retrieve data from a server on a different domain.
2. The web page creates a new **<script>** element and sets its **src** attribute to the URL of the external server's resource. This URL typically includes a callback parameter to specify the name of a JavaScript function to be executed when the response is received.
3. The server on the external domain wraps the data it wants to send in a function call with the specified callback function's name. The data is sent back as a JavaScript script.
4. When the web page's **<script>** tag loads the response, it executes the JavaScript code, calling the specified callback function with the received data as an argument.

## Why JSONP is Used:

JSONP is used for specific scenarios where cross-origin requests are necessary and CORS (Cross-Origin Resource Sharing) headers are not available or not supported. Here are some common use cases and reasons for using JSONP in web development:

1. **Cross-Domain Data Retrieval**: JSONP allows web applications to retrieve data from external servers on different domains, such as fetching data from a third-party API, without encountering the same-origin policy restrictions.
2. **Legacy Browser Support**: JSONP has been used for cross-origin requests in older browsers that do not support modern techniques like CORS. It's a fallback solution for compatibility with older clients.
3. **Embedding Widgets**: JSONP is often used for embedding third-party widgets or content, like social media feeds or advertisements, into a web page. It allows external scripts to be loaded and executed in a controlled way.
4. **Simple Implementation**: JSONP is straightforward to implement and does not require server configurations or additional HTTP headers, making it an accessible method for quick cross-origin data retrieval.

## Limitations of JSONP:

JSONP has some limitations and security concerns:

1. **Security Risks**: JSONP can be vulnerable to security risks, such as cross-site scripting (XSS) attacks, as it executes arbitrary code received from external sources.
2. **Limited Error Handling**: JSONP lacks proper error handling. If the external server fails to respond with valid JSONP data, it can be challenging to detect and handle errors.
3. **Only Supports GET Requests**: JSONP is limited to making GET requests only, as it relies on **<script>** tags to load data.
4. **Unpredictable Response Time**: Because JSONP relies on script tags, there's no way to determine when the response will be received, potentially causing unpredictability in data retrieval.

In summary, JSONP is a technique used in web development to bypass the same-origin policy for cross-domain data retrieval, especially in scenarios where CORS is not an option. However, it should be used with caution and is becoming less common due to its security and usability limitations, with many developers opting for CORS when available.


---

Original Source: https://www.mindstick.com/forum/160093/what-is-jsonp-and-why-is-it-used-in-web-development

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
