Angular 5 http帖子不能用PHP

时间:2018-04-17 11:48:29

标签: angular angular5 angular4-forms angular4-httpclient

我有一个用户表单。我需要将用户详细信息发送到后端。所以我尝试了如下所示:

this.http.post("http://localhost/angular5/user-add.php", this.myform.value).subscribe(
            data => {
                console.log(res)
            }
        )

我的后端是PHP并设置CORS,如下所示:

header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization')

如果我运行此代码,我收到以下错误:

ERROR Object { headers: Object, status: 200, statusText: "OK", url: "http://localhost/angular5/user-add.…", ok: false, name: "HttpErrorResponse", message: "Http failure during parsing for htt…", error: Object }  core.js:1665:5

非常感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个:

this.http.post("http://localhost/angular5/user-add.php", this.myform.value, {responseType: 'text'}).subscribe(
            data => {
                console.log(res)
            }
        )

答案 1 :(得分:0)

不是很容易理解,但是请你写一个异常处理程序,以便你可以看到究竟是什么错误。

  getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
  .pipe(
    tap(heroes => this.log(`fetched heroes`)),
    catchError(this.handleError('getHeroes', []))
  );

}

Angular Heros App