---
title: "What is JSONP, and why was it created? Please explain."  
description: "What is JSONP, and why was it created? Please explain."  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/159006/what-is-jsonp-and-why-was-it-created-please-explain  
category: "json"  
tags: ["javascript", "json"]  
reading_time: 3 minutes  

---

# What is JSONP, and why was it created? Please explain.

What is [JSONP](https://www.mindstick.com/forum/160093/what-is-jsonp-and-why-is-it-used-in-web-development), and why was it created? [Please explain](https://www.mindstick.com/forum/344/hi-rohith-i-am-new-for-mvc-please-explain-how-to-set-implicitly-isauthenticated-true).

## Replies

### Reply by Aryan Kumar

JSONP, or JSON with Padding, is a technique for requesting data from a different domain (also known as a cross-domain request) by using a JavaScript callback function. This technique works by sending a request to the remote server with a callback function name as a parameter. The remote server then returns a JSON object that includes the requested data, wrapped in a call to the callback function. The browser then executes the callback function, which can then access the requested data.

JSONP was created because browsers typically block cross-domain requests by default. This is done to prevent malicious websites from accessing data from other websites without the user's consent. JSONP bypasses this restriction by using a JavaScript callback function, which is considered to be a less risky way of accessing data from other domains.

While JSONP can be useful in some cases, it is not considered to be a secure technique. This is because the callback function name is sent to the remote server in the request, which means that the server can see the name of the function and potentially inject malicious code into it. Additionally, JSONP requests can be slow, as they require the browser to load and execute a JavaScript file from the remote server.

As a result, JSONP is not recommended for use in most cases. If you need to make cross-domain requests, it is better to use a more secure technique, such as CORS (Cross-Origin Resource Sharing).

Here is an example of how JSONP can be used to request data from a different domain:

Code snippet

```plaintext
// Define a callback function
function myCallback(data) {
  // Do something with the data
}

// Make a JSONP request
var script = document.createElement("script");
script.src = "https://example.com/api.php?callback=myCallback";
document.body.appendChild(script);
```

In this example, the `myCallback` function is defined as a global function. The `script` element is then created and its `src` attribute is set to the URL of the remote API. The `callback` parameter in the URL is set to the name of the `myCallback` function. When the browser loads the `script` element, it will execute the `myCallback` function with the requested data as its parameter.

The `myCallback` function can then do something with the data, such as displaying it on the page or storing it in a database.


---

Original Source: https://www.mindstick.com/forum/159006/what-is-jsonp-and-why-was-it-created-please-explain

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
