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
2434
votes
10 answers

What is JSONP, and why was it created?

I understand JSON, but not JSONP. Wikipedia's document on JSON is (was) the top search result for JSONP. It says this: JSONP or "JSON with padding" is a JSON extension wherein a prefix is specified as an input argument of the call itself. Huh?…
Cheeso
  • 189,189
  • 101
  • 473
  • 713
594
votes
17 answers

“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

I'm developing a page that pulls images from Flickr and Panoramio via jQuery's AJAX support. The Flickr side is working fine, but when I try to $.get(url, callback) from Panoramio, I see an error in Chrome's console: XMLHttpRequest cannot load…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
425
votes
4 answers

Can anyone explain what JSONP is, in layman terms?

I know JSONP is JSON with padding. I understand what JSON is, and how to use it with jQuery.getJSON(). However, I do not understand the concept of the callback when introducing JSONP. Can anyone explain to me how this works?
Someone
  • 10,405
  • 23
  • 67
  • 100
412
votes
8 answers

What are the differences between JSON and JSONP?

Format wise, file type wise and practical use wise?
Mohammad
  • 7,344
  • 15
  • 48
  • 76
215
votes
9 answers

Error handling in getJSON calls

How can you handle errors in a getJSON call? Im trying to reference a cross-domain script service using jsonp, how do you register an error method?
Ajay
  • 7,378
  • 18
  • 57
  • 75
196
votes
4 answers

Basic example of using .ajax() with JSONP?

Please could someone help me work out how to get started with JSONP? Code: $('document').ready(function() { var pm_url = 'http://twitter.com/status'; pm_url += '/user_timeline/stephenfry.json'; pm_url += '?count=10&callback=photos'; …
simon
  • 5,987
  • 13
  • 31
  • 28
145
votes
7 answers

Access-Control-Allow-Origin error sending a jQuery Post to Google API's

I read a lot for the 'Access-Control-Allow-Origin' error, but I don't understand what I have to fix :( I'm playing with Google Moderator API, but when I try to add new serie I receive: XMLHttpRequest cannot load…
rubdottocom
  • 8,110
  • 10
  • 39
  • 59
136
votes
15 answers

JSONP with ASP.NET Web API

I am working on creating a new set of services in ASP.MVC MVC 4 using the Web API. So far, it's great. I have created the service and gotten it to work, and now I am trying to consume it using JQuery. I can get back the JSON string using Fiddler,…
Brian McCord
  • 4,943
  • 7
  • 32
  • 44
130
votes
12 answers

How to make a JSONP request from Javascript without JQuery?

Can I make a cross-domain JSONP request in JavaScript without using jQuery or other external library? I would like to use JavaScript itself and then parse the data and make it an object so I could use it. Do I have to use an external library? If…
Dave
  • 1,301
  • 2
  • 9
  • 3
123
votes
12 answers

IE9 jQuery AJAX with CORS returns "Access is denied"

The following works in all browsers except IE (I'm testing in IE 9). jQuery.support.cors = true; ... $.ajax( url + "messages/postMessageReadByPersonEmail", { crossDomain: true, data: { …
Garrett
  • 11,451
  • 19
  • 85
  • 126
121
votes
2 answers

Best content type to serve JSONP?

I have a webservice that when called without specifying a callback will return a JSON string using application/json as the content type. When a callback is specified it will wrap the JSON string in a callback function, so it's not really valid JSON…
Zach
  • 24,496
  • 9
  • 43
  • 50
114
votes
8 answers

parsing JSONP $http.jsonp() response in angular.js

I am using angular's $http.jsonp() request which is successfully returning json wrapped in a function: var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=jsonp_callback"; $http.jsonp(url). …
akronymn
  • 2,426
  • 4
  • 25
  • 39
112
votes
5 answers

So, JSONP or CORS?

My WebAPI was deployed in the Intranet environment. That means security was not my concern. It seems that CORS is much more friendly to the client and easier to implement. Any other concerns I might have missed?
rhapsodyn
  • 3,272
  • 5
  • 29
  • 36
109
votes
9 answers

CORS header 'Access-Control-Allow-Origin' missing

I'm calling this function from my asp.net form and getting following error on firebug console while calling ajax. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://anotherdomain/test.json. (Reason:…
immayankmodi
  • 8,210
  • 9
  • 38
  • 55
103
votes
7 answers

Post data to JsonP

Is it possible to post data to JsonP? Or does all data have to be passed in the querystring as a GET request? I have alot of data that I need to send to the service, cross domain, and it is too large to send via the querystring What are the options…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
1
2 3
99 100