我有這部分代碼在我的應用語法錯誤:在位置JSON意外標記C^0
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
}
當我嘗試添加在我的應用程序的註釋,它拋出一個錯誤如下:
POST http://localhost:4200/assets/comments.json 404 (Not Found)
SyntaxError: Unexpected token C in JSON at position 0
有人可以幫助我嗎?
完全的SyntaxError堆棧:
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)
我們可以看看你如何調用'addComment'? – YounesM
爲什麼你串行,然後解析? –
您可以附加SyntaxError堆棧,以指示哪個代碼行拋出錯誤?當'addComment'被調用時,'this.model'對象的值是什麼? – shaochuancs