Questions tagged [jsonp]

JSON with Padding (JSONP) is a technique for working around cross-domain Ajax limitations.

JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy. JSONP takes advantage of the fact that browsers do not enforce the same-origin policy on <script> tags.

To prevent cross-domain scripting, browsers normally block XMLHttpRequest (XHR) requests to other domains. JSONP works around this by wrapping or "padding" the JSON response such that it can be executed as a script.

For this reason, both the server and the client must support JSONP. The client adds a new <script> tag to the page, rather than creating an XHR, and typically informs the server what function name should be used for padding:

<script src="http://www.example.com/api/getdata?callback=response"></script>

The server then wraps the JSON response in a call to that function, which must be implemented by the client:

response({
    "example1": "somedata",
    "example2": "moredata"
});

Many client-side Ajax libraries abstract away the details of JSONP requests, allowing client code to treat them as normal JSON requests.

jQuery api example

$.ajax({
    url: 'http://www.example.com/api/getdata',
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
       console.log(json);
    },
    error: function(e) {
       console.log(e);
    }
});

Security Note:

Be sure that you trust the server providing the JSONP response as this method allows the page to return a script that will be executed within the context of your page. Connecting to an untrusted service could give them access to your otherwise secure information.

References

4069 questions
44
votes
1 answer

How to make cross-domain AJAX calls to Google Maps API?

I'm trying to make a jQuery $.getJSON call to the Google Maps Geocoding webservice, but this doesn't work because of cross-domain security issues. I haven't been able to figure it out online, but I've read a bit about Google Javascript API or…
42
votes
6 answers

AJAX call and clean JSON but Syntax Error: missing ; before statement

I am making a cross domain JSONP call using this code: jQuery.ajax({ async: true, url: 'http://mnews.hostoi.com/test.json', dataType: 'jsonp', method: "GET", error: function (jqXHR, textStatus, errorThrown) { …
JZweige
  • 721
  • 2
  • 9
  • 15
41
votes
3 answers

How to make a jsonp POST request that specifies contentType with jQuery?

I need to make a jsonp POST request with the content type 'application/json'. I can get the POST request to the server like this: jQuery.ajax({ type: 'POST', url: url, data: data, success: success, …
Marcus
  • 2,021
  • 2
  • 21
  • 20
40
votes
8 answers

JavaScript: How do I create JSONP?

I have a two domains, example1.com and example2.com From example1.com, I would like call a JSON API I have on example2.com. Knowing that this is not allowed, it occurred to me - this is exactly why JSONP was created. Question is, how do I modify my…
Teddi
  • 491
  • 2
  • 6
  • 5
39
votes
3 answers

What is the difference between AJAX, RESTful/Rest, JSON and JSONP?

I am just confused with the these terms. Can anybody please provide/explain me brief with an example?
user901620
39
votes
5 answers

JSONP request error handling

I'm making an ajax jsonp request, but the failure error handling wont work. If the request is 404 or 500 it won't handle the error. I've been looking around to find an answer to this, but can't find anything. There seems to be a solution with…
Adrian Mojica
  • 3,993
  • 4
  • 17
  • 20
37
votes
4 answers

How to make a simple JSONP asynchronous request in Angular 2?

I'm trying to convert the following Angular 1 code to Angular 2: $http.jsonp('https://accounts.google.com/logout'); It needs to be a JSONP request to skip the CORS policy issue.
nunoarruda
  • 2,679
  • 5
  • 26
  • 50
37
votes
2 answers

How to get a JSON response for ItemLookup/Search using Amazon Product Advertising API

I'm trying to get the details of Amazon product from its ASIN. The product API allows to do a Itemlookup with ASIN, but the return value is in XML. I want to do this call for Itemlookup from client side, so would like to do a JSONP call, which I…
thomastinu
  • 399
  • 2
  • 5
  • 5
33
votes
1 answer

Best long term choice: JSONP vs EasyXDM

What is the best long term choice between JSONP and EasyXDM to allow a domain on http to talk to the same domain on https (cross-protocol)? For example, I would like that http://mywebsite.com talks to https://mywebsite.com because the session cookie…
Malartre
  • 1,511
  • 18
  • 28
32
votes
1 answer

Does JSONP require server modifications?

I understand that jsonp is a technique to get around the same origin policy. You basically refer to your json serving server endpoint in a script tag, because script tags are exempt from the SO policy. My question is: Assuming a server has an…
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
30
votes
3 answers

Callback function for JSONP with jQuery AJAX

I didn't quite understand how to work with the callback for the ajax function of jQuery. I have the following code in the JavaScript: try { $.ajax({ url: 'http://url.of.my.server/submit?callback=?', cache: false, type:…
Bani
  • 1,149
  • 2
  • 10
  • 17
30
votes
1 answer

Why same origin policy for XMLHttpRequest

Why do browsers apply the same origin policy to XMLHttpRequest? It's really inconvenient for developers, but it appears it does little in actually stopping hackers. There are workarounds, they can still include javascript from outside sources (the…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
29
votes
5 answers

JavaScript XMLHttpRequest using JsonP

I want to send request parameters to other domain I already know that Cross Scripting needs JsonP and I have used JsonP with Jquery ajax but i do not figure out how to do Cross Scripting as using XMLHttpRequest following code my basic…
happenask
  • 1,062
  • 3
  • 14
  • 21
29
votes
5 answers

"NS_ERROR_DOM_BAD_URI: Access to restricted URI denied"

I have an html-file with several d3-graphs directly written in script tags into it. When I outsource one of the graphs into an external js file I get this message "NS_ERROR_DOM_BAD_URI: Access to restricted URI denied". If I delete the code with…
Schnodderbalken
  • 3,257
  • 4
  • 34
  • 60
29
votes
2 answers

Chrome now blocking all jsonp requests from https to http?

At some point recently Chrome has stopped showing data loaded via jsonp with the error [blocked] The page at https://user.example.com/category/12345 ran insecure content from…
Andy C
  • 795
  • 1
  • 9
  • 10