1

I know that it is a common problem, but I still don't understand why it is reproduced in my case. Сouldn't find an answer in a few days.

Folowing a part of the Angular service:

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Response, RequestOptionsArgs, ResponseContentType } from '@angular/http';
import { Http, RequestOptions, Request, RequestMethod, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

@Injectable()
export class BaseService {
    private headers: Headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8', 'Data-Type': 'json' });

    constructor(private http: Http, private router: Router, ) {
    }

    public get<T>(url: string, options?: RequestOptionsArgs): Promise<T> {
        return this.http.get(url, options)
            .toPromise()
            .then(response => response.json() as T)
            .catch(this.handleError.bind(this));
    }

    public handleError(error: any) {
        if (error.status === 400) {
            switch (error.statusText) {
                case 'Authentication':
                    this.router.navigate(['/error/auth']);
                    break;
                default:
                    return Promise.reject(error);
            }
            return;
        }
        return Promise.reject(error.toString());
    }
}

Folowing method called base method:

getSearchResultTable(hash: number): Promise<Prices[]> {
    const tmp = this.baseService.get<Prices[]>(this.apiUrl + hash);
     return tmp;
}

Json that's returned from server is valid. I checked it one some online service.

But I see SyntaxError: Unexpected end of input error in Response when trying to debug.

Call Stack:

SyntaxError: Unexpected end of input
    at BaseService.get (webpack-internal:///../../../../../src/app/search-module/services/base.service.ts:36:14)
    at SearchResultService.getSearchResultTable (webpack-internal:///../../../../../src/app/search-module/services/search-result.service.ts:30:36)
    at SearchResultPageComponent.ngOnInit (webpack-internal:///../../../../../src/app/search-module/pages/search-result/search-result.component.ts:24:34)
    at checkAndUpdateDirectiveInline (webpack-internal:///../../../core/esm5/core.js:12291:19)
    at checkAndUpdateNodeInline (webpack-internal:///../../../core/esm5/core.js:13794:20)
    at checkAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:13737:16)
    at debugCheckAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:14609:76)
    at debugCheckDirectivesFn (webpack-internal:///../../../core/esm5/core.js:14550:13)
    at Object.eval [as updateDirectives] (ng:///SearchModule/SearchResultPageComponent_Host.ngfactory.js:9:5)
    at Object.debugUpdateDirectives [as updateDirectives] (webpack-internal:///../../../core/esm5/core.js:14535:21)

I'm using latest version of Angular.

1 Answer 1

1

From angular latest update:

remove any map(res => res.json()) calls, which are no longer needed.

So try to remove .json() from .then(response => response.json() as T).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. But how bind result to Prices [] if this remove?
try .then(response => response as T)
It is retur error. error TS2352: Type 'Response' cannot be converted to type 'T'.
It can be the syntax problem in your HTML files also. I found the same problem when my HTML was not correctly formatted. Try basic HTML instead of the big HTML file. I think that should solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.