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
89
votes
1 answer

jsonp with jquery

Can you give a very simple example of reading a jsonp request with jquery? I just can't get it to work.
akula1001
  • 4,576
  • 12
  • 43
  • 56
89
votes
7 answers

jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

To add some basic error handling, I wanted to rewrite a piece of code that used jQuery's $.getJSON to pull in some photo's from Flickr. The reason for doing this is that $.getJSON doesn't provide error handling or work with timeouts. Since $.getJSON…
Matijs
  • 3,118
  • 2
  • 29
  • 41
79
votes
5 answers

JSON string to JS object

I am using a JS object to create graphs with Google visualization. I am trying to design the data source. At first, I created a JS object client-side. var JSONObject = { cols: [{ id: 'date', label: 'Date', type: 'date' }, …
Vinod
  • 31,933
  • 35
  • 96
  • 119
77
votes
10 answers

How do I catch jQuery $.getJSON (or $.ajax with datatype set to 'jsonp') error when using JSONP?

Is it possible to catch an error when using JSONP with jQuery? I've tried both the $.getJSON and $.ajax methods but neither will catch the 404 error I'm testing. Here is what I've tried (keep in mind that these all work successfully, but I want to…
Andy May
  • 4,030
  • 5
  • 34
  • 33
72
votes
7 answers

ASP.net MVC returning JSONP

I am looking to return some JSON across domains and I understand that the way to do this is through JSONP rather than pure JSON. I am using ASP.net MVC so I was thinking about just extending the JsonResult type and then extending the Controller so…
stimms
  • 42,945
  • 30
  • 96
  • 149
70
votes
4 answers

Is JSONP safe to use?

Are there any security issues that should be considered when using JSONP?
dits59
69
votes
11 answers

AJAX cross domain call

I know about AJAX cross-domain policy. So I can't just call "http://www.google.com" over a ajax HTTP request and display the results somewhere on my site. I tried it with dataType "jsonp", that actually would work, but I get a syntax error…
jAndy
  • 231,737
  • 57
  • 305
  • 359
60
votes
9 answers

parsererror after jQuery.ajax request with jsonp content type

I am using jQuery Version 1.5.1 to do the following ajax call: $.ajax({ dataType: 'jsonp', data: { api_key : apiKey }, url: "http://de.dawanda.com/api/v1/" + resource + ".json", success: function(data) { console.log(data); }, …
Thomas
  • 10,289
  • 13
  • 39
  • 55
58
votes
9 answers

jquery how to use multiple ajax calls one after the end of the other

I am in mobile app and I use multiple Ajax calls to receive data from web server like below function get_json() { $(document).ready(function() { $.ajax({ url: 'http://www.xxxxxxxxxxxxx', data: { …
kosbou
  • 3,919
  • 8
  • 31
  • 36
56
votes
6 answers

How to solve JSON_ERROR_UTF8 error in php json_decode?

I am trying this code $json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e"); print_r(json_decode(utf8_encode($json), true)); ////////////// // Define the errors. $constants =…
James Harzs
  • 1,853
  • 5
  • 21
  • 30
55
votes
7 answers

Simple jQuery, PHP and JSONP example?

I am facing the same-origin policy problem, and by researching the subject, I found that the best way for my particular project would be to use JSONP to do cross-origin requests. I've been reading this article from IBM about JSONP, however I am not…
Jeff
  • 12,085
  • 12
  • 82
  • 152
55
votes
6 answers

How to use type: "POST" in jsonp ajax call

I am using JQuery ajax jsonp. I have got below JQuery Code: $.ajax({ type:"GET", url: "Login.aspx", // Send the login info to this page data: str, dataType: "jsonp", timeout: 200000, …
Manoj Singh
  • 7,569
  • 34
  • 119
  • 198
53
votes
9 answers

Javascript search inside a JSON object

I had a JSON string / object in my application. {"list": [ {"name":"my Name","id":12,"type":"car owner"}, {"name":"my Name2","id":13,"type":"car owner2"}, {"name":"my Name4","id":14,"type":"car owner3"}, {"name":"my…
ramesh
  • 4,008
  • 13
  • 72
  • 117
48
votes
3 answers

JSONP call showing "Uncaught SyntaxError: Unexpected token : "

Here is my code $.ajax({ url: 'https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=?', dataType: 'JSONP', jsonpCallback:…
Katakam Nikhil
  • 1,375
  • 4
  • 14
  • 22
45
votes
13 answers

How to parse link header from github API

the github API sends the pagination data for the json results in the http link header: Link: ; rel="next", ; rel="last" since the github API is not…
toxinlabs
  • 942
  • 1
  • 10
  • 14