Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 80801fd

Browse files
committed
feat($http): setting JSONP callback name
1 parent f810940 commit 80801fd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/ng/httpBackend.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
3838
url = url || $browser.url();
3939

4040
if (lowercase(method) == 'jsonp') {
41-
var callbackId = '_' + (callbacks.counter++).toString(36);
41+
var callbackId = (/JSON_CALLBACK\((.*)\)/).exec(url) || ('_' + (callbacks.counter++).toString(36));
42+
43+
if (callbackId.constructor.name === "Array") {
44+
callbackId = callbackId[1];
45+
}
46+
4247
callbacks[callbackId] = function(data) {
4348
callbacks[callbackId].data = data;
4449
};
45-
46-
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
50+
51+
var jsonpDone = jsonpReq(url.replace(/JSON_CALLBACK\((.*)\)|JSON_CALLBACK/, 'angular.callbacks.' + callbackId),
4752
function() {
4853
if (callbacks[callbackId].data) {
4954
completeRequest(callback, 200, callbacks[callbackId].data);

test/ng/httpBackendSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ describe('$httpBackend', function() {
327327
expect(callback).toHaveBeenCalledOnce();
328328
});
329329

330+
it('should set the callback name when use JSONP_CALLBACK with a parameter', function() {
331+
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK(someCallback)', null, callback);
332+
var script = fakeDocument.$$scripts.shift(),
333+
url = script.src.match(SCRIPT_URL);
334+
335+
expect(url[1]).toBe('http://example.org/path');
336+
expect(url[2]).toBe('someCallback');
337+
});
330338

331339
// TODO(vojta): test whether it fires "async-start"
332340
// TODO(vojta): test whether it fires "async-end" on both success and error

0 commit comments

Comments
 (0)