【问题标题】:Angular 5 http post not working with PHPAngular 5 http post 不适用于 PHP
【发布时间】:2018-04-17 11:48:29
【问题描述】:

我有一个用户表单。我需要将用户详细信息发送到后端。于是我尝试如下图:

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

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

【问题讨论】:

    标签: angular angular5 angular4-forms angular4-httpclient


    【解决方案1】:

    试试这个:

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

    【讨论】:

    【解决方案2】:

    不是很容易理解,但是请您编写一个异常处理程序,以便您可以查看到底是什么问题。

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

    }

    Angular Heros App

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多