Search code examples
angulartypescriptmozillaangular2-http

Mozilla cant Parse JSON


My Angular2 (RC4) gets data from my WepApi using following snippet:

getAppointment(id: number): Observable<Event> {
    return this._http.get(this._serviceUrl + 'get/' + id)
        .map(this.extractData)
        .catch(this.handleError);
}

the this.extractData does following:

private extractData(res: Response) {
        let body = res.json();
        return body || {};
}

This all works as expected in Chrome, IE, Edge, but fails in Mozilla throwing this error:

JSON.parse: unexpected character at line 1 column 1 of the JSON data

this leads back to the @angluar2/http module static_response.d.ts.

/**
 * Attempts to return body as parsed `JSON` object, or raises an exception.
 */
json(): any;

and thus in static_response.d.js:

/**
 * Attempts to return body as parsed `JSON` object, or raises an exception.
 */
Response.prototype.json = function () {
    var jsonResponse;
    if (http_utils_1.isJsObject(this._body)) {
        jsonResponse = this._body;
    }
    else if (lang_1.isString(this._body)) {
        jsonResponse = lang_1.Json.parse(this._body);
    }
    return jsonResponse;
};

Why doesnt this work in Mozilla? Is this a bug in @angular2/http?


Solution

  • I am also getting same issue.

    But I have resolve by changing the browser config and it work.

    do this (its not proper way but it will work).

    Open about:config in tab of FF. Than find Network.http.accept.default.

    value =text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

    new Value=text/html,application/xhtml+xml,application/JSON;q=0.9,/;q=0.8