[英]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.