语法错误:JSON 中的意外标记 C 位于位置 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42676564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected token C in JSON at position 0
提问by Dea
I have this part of code in my application
我的应用程序中有这部分代码
addComment (body: Object): Observable<Comment[]> {
//let bodyString = JSON.stringify(body); // Stringify payload
let bodyString = JSON.parse(JSON.stringify(body || null ))
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post(this.commentsUrl, bodyString, options) // ...using post request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
When I try to add a comment in my application it throws an error as below:
当我尝试在我的应用程序中添加评论时,它会引发如下错误:
POST http://localhost:4200/assets/comments.json404 (Not Found)
SyntaxError: Unexpected token C in JSON at position 0
POST http://localhost:4200/assets/comments.json404 (Not Found)
SyntaxError: Unexpected token C in JSON at position 0
Someone can help me?
有人可以帮助我吗?
Fully SyntaxError stack:
完全语法错误堆栈:
SyntaxError: Unexpected token C in JSON at position 0 at Object.parse () at Response.Body.json (body.js:24) at CatchSubscriber.selector (comment.service.ts:41) at CatchSubscriber.error (catch.js:104) at MapSubscriber.Subscriber._error (Subscriber.js:128) at MapSubscriber.Subscriber.error (Subscriber.js:102) at XMLHttpRequest.onLoad (xhr_backend.js:82) at ZoneDelegate.webpackJsonp.1301.ZoneDelegate.invokeTask (zone.js:363) at Object.onInvokeTask (ng_zone.js:264) at ZoneDelegate.webpackJsonp.1301.ZoneDelegate.invokeTask (zone.js:362) at Zone.webpackJsonp.1301.Zone.runTask (zone.js:166) at XMLHttpRequest.ZoneTask.invoke (zone.js:416)
SyntaxError: JSON 中的意外标记 C 位于 Object.parse () at Response.Body.json (body.js:24) at CatchSubscriber.selector (comment.service.ts:41) at CatchSubscriber.error (catch.js) :104) 在 MapSubscriber.Subscriber._error (Subscriber.js:128) 在 MapSubscriber.Subscriber.error (Subscriber.js:102) 在 XMLHttpRequest.onLoad (xhr_backend.js:82) 在 ZoneDelegate.webpackJsonp.1301.ZoneDelegate.invokeTask (zone.js:363) 在 Object.onInvokeTask (ng_zone.js:264) 在 ZoneDelegate.webpackJsonp.1301.ZoneDelegate.invokeTask (zone.js:362) 在 Zone.webpackJsonp.1301.Zone.runTask (zone.js: 166) 在 XMLHttpRequest.ZoneTask.invoke (zone.js:416)
回答by Udlei Nati
I was facing the exactly issue.
我正面临着这个问题。
Just change:
只是改变:
error.json()
to
到
JSON.parse(JSON.stringify(error))
回答by Jose Kj
I got similar error when my php file contained connection successfull message. When i removed all echo commands expect that of json_encode(),i got it working So Make sure that in your php file there is only echo of echo json_encode($data)
当我的 php 文件包含连接成功消息时,我遇到了类似的错误。当我删除所有期待 json_encode() 的 echo 命令时,我让它工作了所以请确保在您的 php 文件中只有echo json_encode($data)
回答by afeef
can you try with this code
你能试试这个代码吗
example
例子
addComment(user: ApiDashboard) {
return this.http.post(this.commentsUrl,user).map(response =>
response.json());
}
you can try with
你可以试试
addComment(user: Comment) {
return this.http.post(this.commentsUrl,user).map(response =>
response.json());
}
here Comment is model file
这里评论是模型文件
for example
例如
export class ApiDashboard {
id: string;
name: string;
price: number;
}
- this case appears when you have printed some values in service file in my case
- 在我的情况下,当您在服务文件中打印了一些值时,就会出现这种情况
service.ts
服务.ts
private handleError(error: any): Promise<any> {
//console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
i commented console.log() then it may works fine can you try solution.
我评论了 console.log() 那么它可能工作正常,你可以尝试解决方案。
回答by amandala
I have had a similar issue when I attempt to send an empty body in the request. Can you confirm that the body being sent is not undefined?
当我尝试在请求中发送空正文时,我遇到了类似的问题。你能确认发送的正文不是未定义的吗?
You could try adding a condition around the post that checks if the body contains something before sending.
您可以尝试在帖子周围添加一个条件,以在发送前检查正文是否包含某些内容。

