views:

9847

answers:

4

hi !

in my eternal internal fight on whether to stay with mootools or jump to jQuery I've found on the jQuery documentation something that got my attention and this is that jQuery can ask for a JSON to a different domain, which is usually forbidden by the browser.

I've seen some workarounds for cross-subdomain, but never cross-domain, and I'm really thrilled, first I thought I was server related but experimenting a little bit more I've seend that doing the very same JSON request from jQuery docs on Mootools doesn't work!

This works jQuery:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });

This doesn't Mootools:

var jsonRequest = new Request.JSON({url: "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&amp;tagmode=any&amp;format=json&amp;jsoncallback=?", onComplete: function(person, responseText){
    alert(responseText);
}}).get({});

How can I replicate this behavior ? what causes it ?

jQuery Doc: http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback Mootols Doc: http://mootools.net/docs/Request/Request.JSON

A: 

It seems you can't do it with Mootools, according to its API docs and this forum.

The reason this is limited is probably because of Cross-site scripting attacks.

Seb
+3  A: 

It's said right on the page that it's JSONP.

JSONP is a trick where the server, instead of returning the usual response, wraps it into a method call of the user-supplied method, e.g. instead of:

{"foo": "bar", "baz":"bah"}

It would return:

temporaryCallbackFunctionName({"foo": "bar", "baz":"bah"});

jQuery defines the temporary callback function and inserts a <script src="..."></script> element, which is not limited by the same origin policy.

When the script is loaded, the function is executed and that's it.

The downside is that if the server is evil (or hacked) it can now execute arbitrary code in your browser.

More info here.

Jaka Jančar
I just read that, it brilliant ! the only problem would be the prepending :P
PERR0_HUNTER
+3  A: 

You may use JSONP in MooTools by using a plugin, JSONP. It's made by Aaron Newton, one of the core MooTools developers.

moff
looks great thanks
PERR0_HUNTER
A: 

This is included in MooTools more since v1.2.2 (released on April 23rd 2009).

Check this documentation page for more info.

benlenarts