1

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?

Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
Paul
  • 161
  • 2
  • 13

2 Answers2

0

Since this is too long for a comment, I added this as an answer - if it doesn't work I'll remove it.


You could try and explicitly add the Accept header, to tell the server you expect a JSON string as response so you don't accidently get XML, like so:

getAppointment(id: number): Observable<Event> {
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    let options = new RequestOptions({ headers: headers });

    return this._http.get(this._serviceUrl + 'get/' + id, options)
        .map(this.extractData)
        .catch(this.handleError);
}
Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
0

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

Pravin Tukadiya
  • 489
  • 4
  • 20