我的应用程序中有这部分代码
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: 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)
我正面临着确切的问题。
只需更改:
error.json()
到
JSON.parse(JSON.stringify(error))
您可能会重复以 C 开头的内容,例如 Connected 您应该仅回显 json 响应,而不是一般的任何字符串!
我也遇到过类似的问题。我发现 Angular 不支持 Spring Boot 的返回类型函数。 该函数的返回类型为
ResponseEntity<String>
。
return ResponseEntity.ok().body(new String("Some Message"));
我将其更改为
ResponseEntity<ResponseMessage>
并将返回修改为
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage("Some Message"));
它奏效了。
ResponseMessage
类包含
public class ResponseMessage {
private String message;
public ResponseMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
感谢 Stackoverflow 社区。这是我的第一个回答。
当我尝试在请求中发送空正文时,我遇到了类似的问题。您能确认发送的正文不是未定义的吗?
您可以尝试在帖子周围添加一个条件,在发送之前检查正文是否包含某些内容。
当我的 php 文件包含连接成功消息时,我遇到类似的错误。当我删除所有期望 json_encode() 的 echo 命令时,我得到了它的工作所以请确保在你的 php 文件中只有
echo json_encode($data)
的 echo
你可以尝试使用这个代码吗
示例
addComment(user: ApiDashboard) {
return this.http.post(this.commentsUrl,user).map(response =>
response.json());
}
你可以尝试使用
addComment(user: Comment) {
return this.http.post(this.commentsUrl,user).map(response =>
response.json());
}
此处注释为模型文件
例如
export class ApiDashboard {
id: string;
name: string;
price: number;
}
服务.ts
private handleError(error: any): Promise<any> {
//console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
我评论了 console.log() 那么它可能工作正常,你可以尝试解决方案吗?
if (text.startsWith("Connected successfully")) {
// If it does, remove this part and parse the remaining JSON
const jsonText = text.substring("Connected successfully".length);
const jsonData = JSON.parse(jsonText);
resolve(jsonData);
我面临着同样的错误 SyntaxError: Unexpected token C in JSON atposition 0 所以我从响应中删除了不需要的部分并仅处理 JSON 数据。