我目前正在开发一个Angular 4聊天应用程序,它可以发出多个HTTP请求。
在Google Chrome或Opera浏览器上,所有请求都能正常运行,但是当我在Firefox上测试我的应用时,其中一个请求无法通过。我已经多次清理了缓存,但问题仍然存在。
this.chatWebService.getLatestConversations(params).subscribe(
(response: CustomHttpResponse<ConversationModel[]>) => {
this.chatSharedService.setDiscussionList(response.data);
},
(errorResponse: HttpErrorResponse) => {
console.log(errorResponse)
});
请求:
public getLatestConversations(p: { filter: string, search: string, page_index: number, page_count: number }): Observable<{}> {
const url = `${this.messagesUrl}?filter=${p.filter}&search=${p.search}&page_index=${p.page_index}&page_count=${p.page_count}`;
return this.http.get(url, { params: this.createHttpParams(p) });
}
private createHttpParams(params: {}): HttpParams {
const searchParams = new HttpParams();
const keys = Object.keys(params);
keys.forEach(key => searchParams.append(key, String(params[key])));
return searchParams;
}
在Firefox上,此代码给出了这个错误:
SyntaxError:JSON.parse:第1行第1列的意外数据结尾 JSON数据
以下是返回的JSON数据的示例:
{
"data": [
{
"type": "user",
"id": 76,
"name": "User 1",
"slug": "razx6k5nuser",
"muted": false,
"messages": [
{
"content": "Hi there",
"media": false,
"sent": 1516372902,
"read": true,
"sender": {
"id": 76,
"slug": "razx6k5nuser",
"lastname": "User lastname",
"firstname": "User first name",
"phoneNumber": ""
}
}
]
},
{
"type": "user",
"id": 71,
"name": "User 2",
"slug": "volc6p1suser",
"muted": false,
"messages": [
{
"content": "new message",
"media": false,
"sent": 1516358686,
"read": false,
"sender": {
"id": 77,
"slug": "razv6i2auser",
"lastname": "User lastname",
"firstname": "User first name",
"phoneNumber": ""
}
}
]
}
],
"meta": {
"page": "1",
"total": 18,
"totalNotRead": 1,
"totalMuted": 0
}
}
根据我在Google上发现的一些错误,似乎可以通过为每个请求添加一些标头来解决,所以我已经使用拦截器在每个请求上添加了一些标头,如下所示:
const request = req.clone({
setHeaders: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
但它仍然无法奏效。有人能帮我吗?提前谢谢。