Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support predefined callback #7

Merged
merged 1 commit into from

2 participants

@rboyce

In order to make OAuth signed requests, the callback parameter needs to be known before a signature can be generated. In this patch, if the callback parameter is already set, AJAX.JSONRequest will respect that rather than overwriting it. This leaves the developer with the responsibility of avoiding global namespace collisions/timing issues, but makes OAuth-ed JSONP a possibility.

@dandean dandean merged commit 7eeb2f8 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 5, 2011
  1. @rboyce
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 2 deletions.
  1. +13 −0 README.md
  2. +12 −2 jsonp.js
View
13 README.md
@@ -105,6 +105,19 @@ new Ajax.JSONRequest('http://api.flickr.com/services/feeds/photos_public.gne', {
});
</pre>
+Making signed requests
+----------------------
+
+To make a signed request (such as an OAuth request), it is generally necessary to
+fix the callback parameter before generating the request signature. To support this,
+if the property corresponding to `callbackParamName` is defined in the `parameters`
+object, that callback will be used instead of an automatically generated one.
+
+When providing your own callback parameter, keep the asynchronous aspect of these
+requests in mind. It is possible for two requests with the same callback parameter
+to overwrite each other; therefore, it is recommended that you make these callback
+parameters unique for any requests that may occur concurrently.
+
Credits
=======
View
14 jsonp.js 100644 → 100755
@@ -4,13 +4,14 @@
* Might as well just call this an iteration.
*
* This version introduces:
+ * - Support for predefined callbacks (Necessary for OAuth signed requests, by @rboyce)
* - Partial integration with Ajax.Responders (Thanks to @sr3d for the kick in this direction)
* - Compatibility with Prototype 1.7 (Thanks to @soung3 for the bug report)
* - Will not break if page lacks a <head> element
*
* See examples in README for usage
*
- * VERSION 1.1.1
+ * VERSION 1.1.2
*
* new Ajax.JSONRequest(url, options);
* - url (String): JSON-P endpoint url.
@@ -67,7 +68,16 @@ Ajax.JSONRequest = Class.create(Ajax.Base, (function() {
Ajax.Responders.dispatch('onComplete', this, response);
}.bind(this);
- // Add callback as a parameter and build request URL
+ // If the callback parameter is already defined, use that
+ if (this.options.parameters[key] !== undefined) {
+ name = this.options.parameters[key];
+ }
+ // Otherwise, add callback as a parameter
+ else {
+ this.options.parameters[key] = name;
+ }
+
+ // Build request URL
this.options.parameters[key] = name;
var url = this.options.url + ((this.options.url.include('?') ? '&' : '?') + Object.toQueryString(this.options.parameters));
Something went wrong with that request. Please try again.