Now in the BER: JSONP!

33 views
Skip to first unread message

Adam Tuttle

unread,
Jun 11, 2013, 5:07:23 PM6/11/13
to taffy...@googlegroups.com
This is undocumented as yet, but I just pushed it into master. I welcome any and all feedback. This is my first (second?) foray into jsonp, so there's a fair chance I'm not doing it right...

New setting: variables.framework.jsonp -- default value = false.

Change its value to a string, which will be the NAME of the request parameter (e.g. url param for GET requests) of the callback method wrapper... so given:

//This is probably the most common use, but it could just as easily be "goat"
variables.framework.jsonp = "callback";


You would receive the same result as:


... except it would be wrapped in a call to zomgdude();

Such that if the response to the former was:

{"hello":"world"}

Then the response to the latter would be:

zomgdude({"hello":"world"});

---

So please feel free to play and test, report bugs, etc. Am I doing it completely wrong? Tell me that too!

You can see the changes I made to implement it, here: https://github.com/atuttle/Taffy/commit/9536dbd8

The only assumption I can think of that this implementation makes is:
  • If you enable jsonp (by setting variables.framework.jsonp to anything other than false) then ANY time there's a request argument (url param or otherwise) with THAT name, the response WILL be wrapped in a callback of that name, regardless of the requested response type. This means that if your API supports BOTH XML and JSON(P) _and_ someone requests XML _and_ they specify a callback name, then they're going to get callback(xml-content)... Not ideal, but really it's their dumb fault. :P
Looking forward to your feedback!

Adam

P.S. ~ Do you think the default should be "callback" and you should have to explicitly disable it by setting it to false?

Phil Cruz

unread,
Aug 26, 2013, 3:50:22 PM8/26/13
to taffy...@googlegroups.com
So I'm trying to use JSONP, the request looks correct. I can see "?callback=JQuery123432..."  In Application.cfc I have

variables.framework.jsonp = true;

in my resource I have

<cffunction name="post" access="public" output="false">
<cfargument name="message" type="string" required="false" default="no message passed"/>
<cfscript>
var result = structNew();
result.message = arguments.message;
return representationOf(result).withStatus(200);

</cfscript>
</cffunction>

But when I examine the response I just see:

{"MESSAGE":"testmessage"}

so the response isn't getting wrapped in the callback. What am I missing?

Thanks,
Phil

Phil Cruz

unread,
Aug 26, 2013, 4:06:20 PM8/26/13
to taffy...@googlegroups.com
I meant Application.cfc has

variables.framework.jsonp = "callback";

-Phil

Adam Tuttle

unread,
Aug 26, 2013, 4:19:50 PM8/26/13
to taffy...@googlegroups.com
Jsonp only works for GET requests. It is accomplished by injecting <script> tags into the document body, so POST,PUT,DELETE are not possible. Try it with a GET request and let me know if it works. :)

Adam


--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Phil Cruz

unread,
Aug 26, 2013, 4:56:20 PM8/26/13
to taffy...@googlegroups.com
Ah, so I see. It does work with a GET request. But if I actually want to do a POST cross-domain request then I can just use

variables.framework.allowCrossDomain = true;

and it works. Awesome.

Thanks,
Phil

Adam Tuttle

unread,
Aug 26, 2013, 5:05:15 PM8/26/13
to taffy...@googlegroups.com
I will have to remember to come back to this discussion when writing the docs for the feature. :)

Adam
Reply all
Reply to author
Forward
0 new messages