Created
October 14, 2011 15:51
-
-
Save tnydwrds/1287473 to your computer and use it in GitHub Desktop.
Quick jQuery Transport sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is currently just a proof of concept. My OAuth usage requires setting | |
// a cookie client-side (server does not do this) after successful authentication. | |
// AIR runtime does not allow XHR to set cookie header so I had to use AIR's | |
// URLRequest object. | |
// | |
// I imagine you can do the same thing but use jsOAuth.request or something in | |
// place of URLRequest | |
// | |
// http://api.jquery.com/extending-ajax/#Transports | |
// Setting up a jQuery transport. | |
$.ajaxTransport('+*',function(options, originalOptions, jqXHR){ | |
return { | |
send: function (headers, completeCallback) { | |
var x = new air.URLRequest(); | |
x.url = options.url | |
x.method = options.type | |
hdrs = [] | |
for (h in headers) { | |
hdrs.push(new air.URLRequestHeader(h, headers[h])); | |
} | |
x.requestHeaders = hdrs | |
var l = new air.URLLoader(); | |
l.addEventListener(air.Event.COMPLETE, completeCallback) | |
l.load(x) | |
}, | |
abort: function () { | |
console.log('Aborted!'); | |
} | |
}; | |
}); | |
// Make jQuery ajax request | |
$.ajax({ | |
type: 'PUT', | |
url: url, | |
headers: {'Cookie':'foo=bar'}, | |
complete: function (data) { | |
console.log('Successfully logged in. User ID: ' + data.text); | |
}, | |
failure: requestFailure | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment