Skip to content

Commit 755afc5

Browse files
committed
fix($httpBackend): don't error when JSONP callback called with no parameter
This change brings Angular's JSONP behaviour closer in line with jQuery's. It will no longer treat a callback called with no data as an error, and will no longer support IE8 via the onreadystatechanged event. BREAKING CHANGE: Previously, the JSONP backend code would support IE8 by relying on the readystatechanged events. This is no longer the case, as these events do not provide adequate useful information for deeming whether or not a response is an error. Previously, a JSONP response which did not pass data into the callback would be given a status of -2, and treated as an error. Now, this situation will instead be given a status of 200, despite the lack of data. This is useful for interaction with certain APIs. Previously, the onload and onerror callbacks were added to the JSONP script tag. These have been replaced with jQuery events, in order to gain access to the event object. This means that it is now difficult to test if the callbacks are registered or not. This is possible with jQuery, using the $.data("events") method, however it is currently impossible with jqLite. This is not expected to break applications. Closes angular#4987 Closes angular#6735
1 parent e987efd commit 755afc5

File tree

2 files changed

+35
-95
lines changed

2 files changed

+35
-95
lines changed

src/ng/httpBackend.js

+31-28
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
4949
var callbackId = '_' + (callbacks.counter++).toString(36);
5050
callbacks[callbackId] = function(data) {
5151
callbacks[callbackId].data = data;
52+
callbacks[callbackId].called = true;
5253
};
5354

5455
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
55-
function() {
56-
if (callbacks[callbackId].data) {
57-
completeRequest(callback, 200, callbacks[callbackId].data);
58-
} else {
59-
completeRequest(callback, status || -2);
60-
}
61-
callbacks[callbackId] = angular.noop;
56+
callbackId, function(status, text) {
57+
completeRequest(callback, status, callbacks[callbackId].data, "", text);
58+
callbacks[callbackId] = noop;
6259
});
6360
} else {
6461

@@ -160,33 +157,39 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
160157
}
161158
};
162159

163-
function jsonpReq(url, done) {
160+
function jsonpReq(url, callbackId, done) {
164161
// we can't use jQuery/jqLite here because jQuery does crazy shit with script elements, e.g.:
165162
// - fetches local scripts via XHR and evals them
166163
// - adds and immediately removes script elements from the document
167-
var script = rawDocument.createElement('script'),
168-
doneWrapper = function() {
169-
script.onreadystatechange = script.onload = script.onerror = null;
170-
rawDocument.body.removeChild(script);
171-
if (done) done();
172-
};
173-
174-
script.type = 'text/javascript';
164+
var script = rawDocument.createElement('script'), callback = null;
165+
script.type = "text/javascript";
175166
script.src = url;
176-
177-
if (msie && msie <= 8) {
178-
script.onreadystatechange = function() {
179-
if (/loaded|complete/.test(script.readyState)) {
180-
doneWrapper();
167+
script.async = true;
168+
169+
callback = function(event) {
170+
removeEventListenerFn(script, "load", callback);
171+
removeEventListenerFn(script, "error", callback);
172+
rawDocument.body.removeChild(script);
173+
script = null;
174+
var status = -1;
175+
var text = "unknown";
176+
177+
if (event) {
178+
if (event.type === "load" && !callbacks[callbackId].called) {
179+
event = { type: "error" };
181180
}
182-
};
183-
} else {
184-
script.onload = script.onerror = function() {
185-
doneWrapper();
186-
};
187-
}
181+
text = event.type;
182+
status = event.type === "error" ? 404 : 200;
183+
}
184+
185+
if (done) {
186+
done(status, text);
187+
}
188+
};
188189

190+
addEventListenerFn(script, "load", callback);
191+
addEventListenerFn(script, "error", callback);
189192
rawDocument.body.appendChild(script);
190-
return doneWrapper;
193+
return callback;
191194
}
192195
}

test/ng/httpBackendSpec.js

+4-67
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ describe('$httpBackend', function() {
3939
fakeDocument = {
4040
$$scripts: [],
4141
createElement: jasmine.createSpy('createElement').andCallFake(function() {
42-
return {};
42+
// Return a proper script element...
43+
return document.createElement(arguments[0]);
4344
}),
4445
body: {
4546
appendChild: jasmine.createSpy('body.appendChild').andCallFake(function(script) {
@@ -351,13 +352,7 @@ describe('$httpBackend', function() {
351352

352353
expect(url[1]).toBe('http://example.org/path');
353354
callbacks[url[2]]('some-data');
354-
355-
if (script.onreadystatechange) {
356-
script.readyState = 'complete';
357-
script.onreadystatechange();
358-
} else {
359-
script.onload();
360-
}
355+
browserTrigger(script, "load");
361356

362357
expect(callback).toHaveBeenCalledOnce();
363358
});
@@ -372,71 +367,13 @@ describe('$httpBackend', function() {
372367
callbackId = script.src.match(SCRIPT_URL)[2];
373368

374369
callbacks[callbackId]('some-data');
375-
376-
if (script.onreadystatechange) {
377-
script.readyState = 'complete';
378-
script.onreadystatechange();
379-
} else {
380-
script.onload();
381-
}
370+
browserTrigger(script, "load");
382371

383372
expect(callbacks[callbackId]).toBe(angular.noop);
384373
expect(fakeDocument.body.removeChild).toHaveBeenCalledOnceWith(script);
385374
});
386375

387376

388-
if(msie<=8) {
389-
390-
it('should attach onreadystatechange handler to the script object', function() {
391-
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop);
392-
393-
expect(fakeDocument.$$scripts[0].onreadystatechange).toEqual(jasmine.any(Function));
394-
395-
var script = fakeDocument.$$scripts[0];
396-
397-
script.readyState = 'complete';
398-
script.onreadystatechange();
399-
400-
expect(script.onreadystatechange).toBe(null);
401-
});
402-
403-
} else {
404-
405-
it('should attach onload and onerror handlers to the script object', function() {
406-
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop);
407-
408-
expect(fakeDocument.$$scripts[0].onload).toEqual(jasmine.any(Function));
409-
expect(fakeDocument.$$scripts[0].onerror).toEqual(jasmine.any(Function));
410-
411-
var script = fakeDocument.$$scripts[0];
412-
script.onload();
413-
414-
expect(script.onload).toBe(null);
415-
expect(script.onerror).toBe(null);
416-
});
417-
418-
}
419-
420-
it('should call callback with status -2 when script fails to load', function() {
421-
callback.andCallFake(function(status, response) {
422-
expect(status).toBe(-2);
423-
expect(response).toBeUndefined();
424-
});
425-
426-
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
427-
expect(fakeDocument.$$scripts.length).toBe(1);
428-
429-
var script = fakeDocument.$$scripts.shift();
430-
if (script.onreadystatechange) {
431-
script.readyState = 'complete';
432-
script.onreadystatechange();
433-
} else {
434-
script.onload();
435-
}
436-
expect(callback).toHaveBeenCalledOnce();
437-
});
438-
439-
440377
it('should set url to current location if not specified or empty string', function() {
441378
$backend('JSONP', undefined, null, callback);
442379
expect(fakeDocument.$$scripts[0].src).toBe($browser.url());

0 commit comments

Comments
 (0)