Hi list, I've been looking for a JSONP implementation in Prototype, but can't seem to find any yet. I came across something from a guy called Cody Swann, but that didn't seem to work, and his website seems to have disappeared. I tried to come up with a nice, integrated way to do it myself, but I'm not sure that it's possible to use the default Prototype Ajax way of doing things. Does anyone have something ready to go, or can someone give me some pointers on how to best implement it? Cheers, Ron Derksen
on 2008-06-17 14:22
on 2008-06-17 16:00
All JSONP is, is an endpoint that returns javascript that is a call to a named function on your page, usually passing the resultant processed data as a JSON parameter to said function. Some JSONP endpoints will have a fixed name for that callback function, and some allow you to specify the name of that function in your request parameters (usually via the query string since JSONP is really designed for consumption via script tags, not Ajax). There is nothing special you have to do on the client side other than have a named function available (which again should match what the JSONP endpoint is returning, or what you pass in to a configurable JSONP endpoint), and allow your XHR layer to eval javascript (which Prototype does; see: http://www.prototypejs.org/api/ajax/options). If you're consuming a JSONP endpoint with Ajax, though, you're kind of defeating one of the main purposes of JSONP. XHR, of course, requires that you follow SOP (same origin policy), which means the XHR calls need to go to the same domain as the current page. JSONP is really designed to be consumed via script tags, thus is a cross domain scripting method primarily. If you're building the backend yourself and aren't building a public service API where you want to enable people to use script tags to make calls cross domain... then for all intents and purposes you really don't need JSONP. So anyway, the answer is you don't need to do anything special, just have a named function available (at the global level) in your page, that the returned javascript can call into. On 6/17/08, Ron Derksen <chevalric@gmail.com> wrote: > to go, or can someone give me some pointers on how to best implement > it? > > Cheers, > > Ron Derksen > > > -- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World's First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl
on 2008-06-17 18:40
I've been playing with JSONP some time ago. Take a look at: http://github.com/kangax/protolicious/tree/master/... - kangax