Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/prototype/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ var Ajax = {
* Represents the number of active XHR requests triggered through
* [[Ajax.Request]], [[Ajax.Updater]], or [[Ajax.PeriodicalUpdater]].
**/
activeRequestCount: 0
activeRequestCount: 0,
_jsonpfunctions: []
};
16 changes: 16 additions & 0 deletions src/prototype/ajax/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ Ajax.Request = Class.create(Ajax.Base, {
params += (params ? '&' : '') + "_method=" + this.method;
this.method = 'post';
}
if (this.options.jsonpparametername) {
this.method = 'get';
var currentfunctioncount = Ajax._jsonpfunctions.length;
Ajax._jsonpfunctions.push(function(data){
(this.options.onComplete || Prototype.emptyFunction)({'responseJSONP':data});
(this.options.onSuccess || Prototype.emptyFunction)({'responseJSONP':data});
$('AJAXJSONP_'+currentfunctioncount) ? $('AJAXJSONP_'+currentfunctioncount).remove() : '';
}.bind(this));
params += (params ? '&' : '') + this.options.jsonpparametername+'=Ajax._jsonpfunctions['+currentfunctioncount+']';
}

if (params && this.method === 'get') {
// when GET, append parameters to URL
Expand All @@ -196,6 +206,12 @@ Ajax.Request = Class.create(Ajax.Base, {

this.parameters = params.toQueryParams();

if (this.options.jsonpparametername) {
document.body.insert(new Element('script',{'type':'text/javascript','src':this.url,'id':'AJAXJSONP_'+(Ajax._jsonpfunctions.length-1)}));
Ajax.Responders.dispatch('onCreate', this, response);
return;
}

try {
var response = new Ajax.Response(this);
if (this.options.onCreate) this.options.onCreate(response);
Expand Down
22 changes: 22 additions & 0 deletions test/unit/tests/ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,28 @@ suite("Ajax", function () {

Ajax.Request.prototype.isSameOrigin = isSameOrigin;
});
//
// Commented out because I'm not sure how to make WEBRICK and Sinatra handle this JSONP request
//
// test("JSONP with Same Origin",function(done){
// new Ajax.Request("/jsonp",extendDefault({
// parameters: "handlejsonp=1",
// jsonpparametername:'callback',
// onSuccess: function(transport) {
// assert.equal(transport.responseJSONP.handlejsonp,"1");
// done();
// }
// }))
// })
test("JSONP with Different Origin",function(done){
new Ajax.Request("http://echo.jsontest.com/key/value/one/two",extendDefault({
jsonpparametername:'callback',
onSuccess: function(transport) {
assert.deepEqual(transport.responseJSONP,{"one": "two","key": "value"});
done();
}
}))
})

});