Search code examples
ngrx-effectsangular4-httpclient

Handle non-JSON data in @ngrx/effects


I don't know how to handle the non-JSON data in @ngrx/effects

I want to parse the text not JSON

//effects
@Effect() getNonJsonData = () => this.actions
        .ofType<GetNonJsonAction>(GET_NON_JSON_DATA)
        .switchMap(() => this.api
            .getNonJsonData()
            .map(getNonJsonDataFulfilled)
            .catch(error => Observable.of(getNonJsonDataRejected(error)))
        )
//api
getNonJsonData(): Observable<string> {
    return this.http.get<string>('my_http_url');// It returns 'ACCESS' or 'NON_ACCESS' string not return JSON
}

It always go to the getNonJsonDataRejected action, its not going to getNonJsonDataFulfilled action.

I'm getting this error message "Http failure during parsing, name:"HttpErrorResponse"

SyntaxError: Unexpected token A in JSON at position 0 at Object.parse (<anonymous>) at XMLHttpRequest.onLoad

Please tell me.


Solution

  • Finally i find out the answer, we need to add the {responseType: 'text'} in the http api call, so the code is,

    getNonJsonData(): Observable<string> {
       return this.http.get('my_http_url', {responseType: 'text'});
    }
    

    ref: https://blog.hackages.io/angular-http-httpclient-same-but-different-86a50bbcc450