Angular &引用;JSON输入意外结束“;角度5

Angular &引用;JSON输入意外结束“;角度5,angular,Angular,当向API发送GET请求时,我总是会收到一个错误(因此响应将落入CATCH而不是TRY)即使艰难的状态是200 请求如下: // Check if user exists at blur on email input authentication checkUserExists( platformId: string, email: string) { return this.http.get(checkUserExistsAPI + `/${platformId}/${email}`)

当向API发送GET请求时,我总是会收到一个错误(因此响应将落入CATCH而不是TRY)即使艰难的状态是200

请求如下:

// Check if user exists at blur on email input authentication
checkUserExists( platformId: string, email: string) {
    return this.http.get(checkUserExistsAPI + `/${platformId}/${email}`);
}
// Verify if emails exists
verifyEmail(email) {
    if (email) {
        this.authenticationService.checkUserExists( this.platformId, email )
            .subscribe(
                (data: CheckEmailResponse) => {
                    console.log(data);
                },
                (error: CheckEmailResponse) => {
                    console.log(error);

                }
            );
    }
}
当我使用Angular 5时,我去掉了.map(res)

下面是使用请求的函数:

// Check if user exists at blur on email input authentication
checkUserExists( platformId: string, email: string) {
    return this.http.get(checkUserExistsAPI + `/${platformId}/${email}`);
}
// Verify if emails exists
verifyEmail(email) {
    if (email) {
        this.authenticationService.checkUserExists( this.platformId, email )
            .subscribe(
                (data: CheckEmailResponse) => {
                    console.log(data);
                },
                (error: CheckEmailResponse) => {
                    console.log(error);

                }
            );
    }
}
它永远不会控制台.log(数据),因为我总是遇到以下错误:

error:{
    error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onLo…, text: ""}
    headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
    message:"Http failure during parsing for http://API.com"
name:"HttpErrorResponse"
ok:false
status:200
statusText:"OK"
错误:{
错误:SyntaxError:在XMLHttpRequest.onLo…的JSON.parse()处,JSON输入意外结束,文本:'}
headers:HttpHeaders{normalizedNames:Map(0),lazyUpdate:null,headers:Map(0)}
消息:“在解析的过程中Http失败。”http://API.com"
名称:“HttpErrorResponse”
好:错
现状:200
状态文本:“确定”
好吧,我想出来了。 响应没有任何内容体,Angular 5在默认情况下需要Json格式的内容体,因此无法工作。我只需要状态响应就可以知道是200还是404

我在请求中添加了
{responseType:'text'}
,如上所述:

返回this.http.get(checkUserExistsAPI+
/${platformId}/${email}
,{responseType:'text'})


因此,现在它设法转到我的TRY/CATCH的TRY,数据等于“null”。但我不关心数据,因为我只关心状态。无论如何,现在如果状态为200,它将进入TRY,如果状态为404,它将进入CATCH。

这就是head请求的目的。当您不需要该对象时,只需要有关该对象的元数据,例如状态代码。见:

您能发布您的回复吗?可能响应无效JSON响应在我的初始帖子中,这是最后一个代码块。这是错误消息,我指的是http get响应中的JSON,您可以在chrome/firefox开发者工具的网络选项卡下获取。实际上,响应没有正文内容。只有状态对我有用。我猜Angular希望响应是json格式的,但不会得到任何内容