r/programming • • Apr 03 '10

How to make cross-domain JSON requests without using a server-side proxy (Javascript)

http://www.caseybanner.ca/2010/04/03/cross-domain-remote-json-in-javascript/
0 Upvotes

6 comments sorted by

2

u/_pixie_ Apr 03 '10

jquery.ajax will do a script/jsonp request automatically if you set the type to 'GET' and the dataType to 'jsonp'. the return type from the call is undefined because unlike xhr, you can't get a handle to cancel the request.

1

u/kcbanner Apr 03 '10

Cool, thanks!

1

u/lol-dongs Apr 03 '10

Yeah, this has been known about for a while and is called JSONP. One problem with it compared with XHR is that it is much harder to handle error conditions and carries quite a few security implications, but for something as frivolous as accessing the Twitter API, that's not a big concern.

1

u/kcbanner Apr 03 '10

Ah, thanks, I didn't know the formal name of this technique. I suppose the remote end could always send you malicious code, but I think generally you would trust an API that you are using.

2

u/[deleted] Apr 03 '10

http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

It's also incorporated into jquery as one of the $.ajax datatypes:

http://api.jquery.com/jQuery.ajax/

0

u/[deleted] Apr 03 '10

It's also a pain in that it isn't possible to make it synchronous.

I've been writing an app that needs to check if a file exists on a remote server. I am using jsonp for this, but it is a pain to get it to wait for a response before moving on.