我在我的应用程序中有这部分代码。
当我尝试在我的应用程序中添加评论时,它会抛出以下错误:
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 (<anonymous>)
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
吗? - YounesMaddComment
时,this.model
对象的值是什么? - shaochuancsResponse.Body.json (body.js:24)
和CatchSubscriber.selector (comment.service.ts:41)
。请问在你的代码中出现了哪一个?它指示的是哪一行? - shaochuancs