语法错误:JSON.parse:Angular中JSON数据的第1行第1列出现意外字符

ryevplcw  于 2023-01-27  发布在  Angular
关注(0)|答案(1)|浏览(200)

我有一个错误语法错误:JSON.parse:JSON数据的第1行第1列出现意外字符
使用验证器json时,我意识到错误在

let json = JSON.stringify(user);
    let params = "json="+json; // here is the error in the "j" of json=

错误:第1行出现分析错误:json = {“名称”:“p ^

json = {
    "name": "pedroeoeo",
    "surname": "oiuoiuoiu",
    "email": "iouoiu@hlkjlkj.com",
    "password": "lkjlkjlkj"
}

但是我需要添加到json = {“name”:“kjlklk”...等等...

register(user):Observable<any>
  {
    let json = JSON.stringify(user);
    console.log(json);
    let params = "json="+json;
    
    console.log(params);

    let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    return this._http.post(this.url+'register', params, {headers:headers});
  }

后端是用laravel做的我用postman测试过了,后端还可以,但是当我添加到一个json变量时,问题是有Angular 的

json={"name":"pedroeoeo","surname":"oiuoiuoiu","email":"iouoiu@hlkjlkj.com","password":"lkjlkjlkj"
```}

I dont know how to fix it

I dont know what can I do to fix the issue
r9f1avp5

r9f1avp51#

正确发送。

register(user):Observable<any>
  {
    let body = { json: user };
    
    let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');

    return this._http.post(this.url+'register', body, {headers:headers});
  }

相关问题