简体   繁体   English

Angular 4 Restful Post响应,带有“ SyntaxError:JSON.parse处的JSON输入意外结束”

[英]Angular 4 Restful Post response with “SyntaxError: Unexpected end of JSON input at JSON.parse”

Below I have the following Angular segment which appears to attempting to handle the response as JSON even though 'httpOptionsText' is a constant defined as: 下面我有以下Angular段,即使'httpOptionsText'是定义为以下常量的常量,它似乎也尝试将响应作为JSON处理:

const httpOptionsText = {
    headers: new HttpHeaders({
    'Content-Type': 'text/plain'
    })
}; 

addOrders(order, rootUri): Promise<DetailOrderbean[]> {
let body = JSON.stringify(order);
const url = rootUri + 'api/data/addDetailOrder?order=' + body;

return this.http.post<DetailOrderbean>(url, body, httpOptionsText)
    .toPromise()
    .then(res => {
           console.log(res);
    })
    .catch(this.handleError);
}

Additionally, the java is specifying 此外,java正在指定

        response.setContentType( "text/plain");
        response.setHeader("Access-Control-Allow-Origin", 
        "http://localhost:4200");
        response.setHeader("Access-Control-Allow-Credentials","true");

The status is 200, so I just want to consume the error. 状态为200,所以我只想消耗该错误。 However, in the event of an actual error I need the handleError as it exits: 但是,如果发生实际错误,我需要在退出时使用handleError:

private handleError(error: any): Promise<any> {
    console.error('An error occurred', error);
    return Promise.reject(error.message || error);
}

I cast a structure inappropriately to the post () which forced the json conversion. 我不适当地将结构强制转换为post(),该结构强制进行json转换。 This could also be seen by the compilar error: 这也可以通过类似的错误看出:

Types of property 'responseType' are incompatible. 属性“ responseType”的类型不兼容。 Type '"text"' is not assignable to type '"json"'. 类型“” text”不能分配给类型““ json””。

This should have told me that something was up with my return response type. 这应该告诉我,我的返回响应类型有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束 - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse 未捕获的 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) Json.parse 抛出:“语法错误:JSON 输入的意外结束” - Json.parse throws: “SyntaxError: Unexpected end of JSON input” 未捕获的语法错误:JSON 输入的意外结束:在 JSON.parse(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input : at JSON.parse (<anonymous>) Uncaught SyntaxError:执行JSON.Parse时输入意外结束 - Uncaught SyntaxError: Unexpected end of input While doing JSON.Parse 语法错误:JSON.parse:数据意外结束 - SyntaxError: JSON.parse: unexpected end of data 语法错误:JSON.parse:数据意外结束 - SyntaxError: JSON.parse: unexpected end of data JSON.parse处的JSON输入意外结束 - Unexpected end of JSON input at JSON.parse 递归 JSON 输入导致语法错误:JSON 输入在 JSON.parse 的意外结束 - Recursive JSON input causes SyntaxError: Unexpected end of JSON input at JSON.parse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM