diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index 416cf552b3..90d4f32912 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -54,9 +54,8 @@ export class XHRConnection implements Connection { let onLoad = () => { // responseText is the old-school way of retrieving response (supported by IE8 & 9) // response/responseType properties were introduced in ResourceLoader Level2 spec (supported - // by - // IE10) - let body = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText; + // by IE10) + let body = _xhr.response === undefined ? _xhr.responseText : _xhr.response; // Implicitly strip a potential XSSI prefix. if (isString(body)) body = body.replace(XSSI_PREFIX, ''); let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders()); diff --git a/modules/@angular/http/test/backends/xhr_backend_spec.ts b/modules/@angular/http/test/backends/xhr_backend_spec.ts index 78cb2bec60..0b74c4b14a 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -686,6 +686,23 @@ Connection: keep-alive`; existingXHRs[0].setStatusCode(statusCode); existingXHRs[0].dispatchEvent('load'); })); + + it('should not throw invalidStateError if response without body and responseType not equal to text', + inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { + const base = new BaseRequestOptions(); + const connection = new XHRConnection( + new Request( + base.merge(new RequestOptions({responseType: ResponseContentType.Json}))), + new MockBrowserXHR()); + + connection.response.subscribe((res: Response) => { + expect(res.json()).toBe(null); + async.done(); + }); + + existingXHRs[0].setStatusCode(204); + existingXHRs[0].dispatchEvent('load'); + })); }); }); }