#208 ✓resolved
Douwe M.

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

    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.

    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

    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.

    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

    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.

    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

    MadRabbit February 28th, 2010 @ 01:48 PM

    I think we could even bend Xhr itself and make it watch say some special option, like jsonp, 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.

    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 with

    http://en.wikipedia.org/w/api.php?action=opensearch&search=something&format=json&callback=s0merand0mg3neratedfunct1on
    

    as the src, and a function s0merand0mg3neratedfunct1on(data) that would set the responseJSON property on the original Xhr object to data. That way the user could just use this.json as he's used to with normal Xhr requests.
    An other option is pointing the s0merand0mg3neratedfunct1on at the onSuccess function, so that the received JSONP data would be provided as an argument to the onSuccess function, but then the user couldn't use this.json like he's used to.

  • MadRabbit

    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 just true 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.

    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 using XHR is the json property, nice work ;)

  • MadRabbit

    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 standard Xhr interface so all the other features are all the same.

  • Douwe M.

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

RightJS Core Tickets

Shared Ticket Bins

People watching this ticket

Tags

Pages