in Technology by
What is JSONP, and what is the use of it?

1 Answer

0 votes
by

It's actually not too complicated...

Say you're on domain example.com, and you want to make a request to domain example.net. To do so, you need to cross domain boundaries, a no-no in most of browserland.

The one item that bypasses this limitation is <script> tags. When you use a script tag, the domain limitation is ignored, but under normal circumstances, you can't really do anything with the results, the script just gets evaluated.

Enter JSONP. When you make your request to a server that is JSONP enabled, you pass a special parameter that tells the server a little bit about your page. That way, the server is able to nicely wrap up its response in a way that your page can handle.

For example, say the server expects a parameter called callback to enable its JSONP capabilities. Then your request would look like:

http://www.example.net/sample.aspx?callback=mycallback

Without JSONP, this might return some basic JavaScript object, like so:

{ foo: 'bar' }

However, with JSONP, when the server receives the "callback" parameter, it wraps up the result a little differently, returning something like this:

mycallback({ foo: 'bar' });

As you can see, it will now invoke the method you specified. So, in your page, you define the callback function:

mycallback = function(data){
  alert(data.foo);
};

And now, when the script is loaded, it'll be evaluated, and your function will be executed. Voila, cross-domain requests!

It's also worth noting the one major issue with JSONP: you lose a lot of control of the request. For example, there is no "nice" way to get proper failure codes back. As a result, you end up using timers to monitor the request, etc, which is always a bit suspect. The proposition for JSONRequest is a great solution to allowing cross domain scripting, maintaining security, and allowing proper control of the request.

Related questions

0 votes
    What is a YAML file and how do we use it in Ansible?...
asked Jul 28, 2021 in Technology by JackTerrance
0 votes
    Which of the following refers to stealing one's idea or invention of others and use it for their own benefits? Piracy Plagiarism Intellectual property rights All of the above...
asked Mar 4, 2021 in Technology by JackTerrance
0 votes
    What Is Vaccum Currcuit Breaker.define With Cause And Where Be Use It Device?...
asked Dec 12, 2020 in Technology by JackTerrance
0 votes
    What Is Acsr Cable And Where We Use It?...
asked Dec 12, 2020 in Technology by JackTerrance
0 votes
    Can we place an Excel file in a shared location and use it to develop a report and refresh it in regular intervals?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    It is wise to use secondary indexes on the columns you want to be querying on has few unique values (1)True (2)False...
asked May 7, 2021 in Technology by JackTerrance
0 votes
    It is wise to use secondary indexes on the columns you want to be querying on has few unique values (1)True (2)False...
asked Apr 16, 2021 in Technology by JackTerrance
0 votes
    If we use a keyword in domain, Will it hit with EMD filter?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    What is Google Webmasters Tools, Why do you use it?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    Is it better to use malloc () or calloc ()?...
asked Jan 23, 2021 in Technology by JackTerrance
+1 vote
    Is it possible to use curly brackets ({}) to enclose a single line code in C program?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    Is it possible to use Kafka without ZooKeeper?...
asked Nov 1, 2020 in Technology by JackTerrance
+1 vote
    Is it possible to create a custom domain name, or use your organisation's domain name such as eduforum.in, in Azure Active Directory?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    When should you apply “next” statement in R? When is it appropriate to use the “next” statement in R?...
asked Oct 16, 2020 in Technology by JackTerrance
...