JSONP support?
Reported by Douwe M. | February 27th, 2010 @ 08:00 PM | in 2.0.0 Release
Does RightJS's Xhr
currently support JSONP? I can't seem
to find anything about in either the documentation or the source
code...
If this is currently not supported, please take this as a feature request :)
Comments and changes to this ticket
-
MadRabbit February 28th, 2010 @ 01:06 PM
- State changed from new to invalid
- Tag changed from jsonp, xhr to xhr
It does not support it and I'm afraid will not. JSONP has a big deal of XSS vulnerability which we are kinda trying to fix enforcing standard JSON responses and checking them with native parsers, regexps and stuff.
JSONP makes sense only in one case, when you need to grab JSON data from another domain, because you cannot make normal XHR requests there and have to use the SCRIPT tag to access the data.
In all the other cases IMO it's a naive hack, and you'll have a better designed app if you won't use it.
But thanks for the offer anyway!
-
Douwe M. February 28th, 2010 @ 01:10 PM
JSONP makes sense only in one case, when you need to grab JSON data from another domain, because you cannot make normal XHR requests there and have to use the SCRIPT tag to access the data. And that's exactly what I want to do... I could of course use a PHP proxy, but why not just use JSONP as so many content providers support it?
-
MadRabbit February 28th, 2010 @ 01:18 PM
Mmm... I probably understood you wrong.
What do you mean? Do you know that you cannot make XHR requests to another domains in any case?
-
Douwe M. February 28th, 2010 @ 01:26 PM
I know that, and to get around that restriction, you can use JSONP, which isn't really Xhr, but has the same effect (kinda). I know why the same origin policy is there, but in some cases directly requesting the Wikipedia, Twitter, Flickr, whatever API using JavaScript does make sense. With regular Xhr this is not possible, and probably will never be, but using JSONP it is. I can of course write my own JSONP processing code, or just use some PHP proxy do do the same, but I think it would be nice to have JSONP support built right into RightJS.
jQuery.ajax()
supports this quite nicely too. -
MadRabbit February 28th, 2010 @ 01:40 PM
- State changed from invalid to new
- Tag changed from xhr to jsonp, xhr
Okay, now I see. You should give more food for thoughts next time 8)
I think it's better to make a plugin then, or at least start with one.
-
Douwe M. February 28th, 2010 @ 01:42 PM
I'm sorry if I wasn't clear at first :P
A plugin would be nice, but I think it should use the same syntax as the current
Xhr
object... With another name and slightly different options of course. -
MadRabbit February 28th, 2010 @ 01:48 PM
I think we could even bend
Xhr
itself and make it watch say some special option, likejsonp
, so your code would be transparent to the feature, kinda like that;Xhr.load('http://foo.boo/hoo.js', { jsonp: true, // or maybe some callback name in here onSuccess: function() { alert(this.json); // you have a normal JSON data in here } });
-
Douwe M. February 28th, 2010 @ 02:01 PM
This is an example of a JSONP URL:
http://en.wikipedia.org/w/api.php?action=opensearch&search=something&format=json&callback=functionname
Where
callback
is the JavaScript function to be called. So you could do something like this:Xhr.load('http://en.wikipedia.org/w/api.php', { params: { action: 'opensearch', search: 'something', format: 'json' }, jsonp: 'callback' //the GET param for the callback function onSuccess: function() { alert(this.json); // you have a normal JSON data in here } });
Then a
<script>
tag would be made withhttp://en.wikipedia.org/w/api.php?action=opensearch&search=something&format=json&callback=s0merand0mg3neratedfunct1on
as the
src
, and a functions0merand0mg3neratedfunct1on(data)
that would set theresponseJSON
property on the originalXhr
object todata
. That way the user could just usethis.json
as he's used to with normalXhr
requests.
An other option is pointing thes0merand0mg3neratedfunct1on
at theonSuccess
function, so that the received JSONP data would be provided as an argument to theonSuccess
function, but then the user couldn't usethis.json
like he's used to. -
MadRabbit June 6th, 2010 @ 07:40 PM
- Milestone set to 2.0.0 Release
- State changed from new to resolved
Hey Douwe!
Sorry, it took me a while, but finally I've realized how right you were 8), and so I've added the feature in the core.
It is already on github and will go public with the next release. The use is simple and just as we talked about. There is a new parameter called
jsonp
, which can be set to the callback parameter name or justtrue
for 'callback'new Xhr('/bla/bla/bla', { jsonp: true }); Xhr.load('/bla/bla/bla', { jsonp: 'paramname' }); // and even like that $('my-form').send({ jsonp: 'bla_bla_bla' });
Enjoy!
And thanks for the idea!
-
Douwe M. June 6th, 2010 @ 08:03 PM
Okay, cool, thanks for looking into it :)
So I guess it works with the
onSuccess
lambda like I proposed in my last post? Cool :) Then the only real difference with usingXHR
is thejson
property, nice work ;) -
MadRabbit June 6th, 2010 @ 08:12 PM
yes, and you can't do things like
onFailure
, cancel the request, specify the method and stuff. but it works in the backend through the standardXhr
interface so all the other features are all the same.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.