0

I have a user form. I need user details sent to back-end. So I tried as shown below:

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

My back-end is PHP and set CORS as shown below:

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')

If I run this code, I am getting the below error:

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

Any help is greatly appreciated. Thanks in advance.

2 Answers 2

2

Try this:

this.http.post("http://localhost/angular5/user-add.php", this.myform.value, {responseType: 'text'}).subscribe(
            data => {
                console.log(res)
            }
        )
Sign up to request clarification or add additional context in comments.

2 Comments

It is working fine. Thanks for the response. Why we need to give responseType here?
For more information, you can read this docs angular.io/guide/http#requesting-non-json-data
0

Not very easy to understand but could you please write a exception handler so that you can see what exactly is wrong.

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

}

Angular Heros App

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.