Skip to content

Commit 38aa441

Browse files
committed
fix($templateRequest): ignore JSON Content-Type header and content
Normally, if there is a Content-Type header with the string "application/json", or else the content looks sort of JSON-y, $http will attempt to deserialize the JSON into an object. $templateRequest is intended to request markup, and as such should never attempt to parse JSON, regardless of the headers or shape of the content. Closes angular#5756
1 parent 6502ab0 commit 38aa441

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ng/templateRequest.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ var $compileMinErr = minErr('$compile');
2121
*/
2222
function $TemplateRequestProvider() {
2323
this.$get = ['$templateCache', '$http', '$q', function($templateCache, $http, $q) {
24+
var httpOptions = {
25+
cache: $templateCache,
26+
transformResponse: null
27+
};
28+
2429
function handleRequestFn(tpl, ignoreRequestError) {
2530
var self = handleRequestFn;
2631
self.totalPendingRequests++;
2732

28-
return $http.get(tpl, { cache : $templateCache })
33+
return $http.get(tpl, httpOptions)
2934
.then(function(response) {
3035
var html = response.data;
3136
if(!html || html.length === 0) {

test/ng/templateRequestSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ describe('$templateRequest', function() {
8686
expect($templateRequest.totalPendingRequests).toBe(0);
8787
}));
8888

89+
it('should not try to parse a response as JSON',
90+
inject(function($templateRequest, $httpBackend) {
91+
var spy = jasmine.createSpy('success');
92+
$httpBackend.expectGET('a.html').respond('{{text}}', {
93+
'Content-Type': 'application/json'
94+
});
95+
$templateRequest('a.html').then(spy);
96+
$httpBackend.flush();
97+
expect(spy).toHaveBeenCalledOnce();
98+
expect(spy.argsForCall[0][0]).toBe('{{text}}');
99+
}));
89100
});

0 commit comments

Comments
 (0)