Note that there are some explanatory texts on larger screens.

plurals
  1. POtrying to understand jsonp with the flickr example
    primarykey
    data
    text
    <p>I'm trying to get my head around how I can make json request to a json file stored on my server from jsfiddle.</p> <p>html:</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="images"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>jquery: </p> <pre><code>$.getJSON("http://www.shopsheep.com/groupon/json/test.json?jsoncallback=?", function(data) { $.each(data.items, function(i, item) { $("&lt;img/&gt;").attr("src", item.media.m).appendTo("#images"); if (i == 0) { return false; } }); }); $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?format=json&amp;jsoncallback=?", function(data) { $.each(data.items, function(i, item) { $("&lt;img/&gt;").attr("src", item.media.m).appendTo("#images"); if (i == 0) return false; }); }); </code></pre> <p>I downloaded the flicker json file and uploaded it to my server as test.json. If I paste it in the browser it returns just as the flicker file.</p> <p>However when I try to display the image only the original flicker example is working? Any idea why this is the case?</p> <p><a href="http://jsfiddle.net/stofke/DJQ5g/" rel="nofollow">http://jsfiddle.net/stofke/DJQ5g</a></p> <p><strong>Ok I have found out how to do this. The getJSON function adds a random named callback functionname to jsoncallback=? something like this <code>jQuery160188050875203142_1309437718540&amp;_=1309437718551</code> In order to wrap your json file with this callbackfunction you need of course to know the name of this function, so if you convert your jsonfile into a php file than you can get the callbackfunctionname like this:</strong></p> <pre><code>&lt;?php echo $_GET["jsoncallback"];?&gt;( ADD JSON CONTENT HERE ) </code></pre> <p><strong>This php file will get the name of the callbackfunction via a GET variable and wraps the json content with it. This way it works fine.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload