angular: Http failure during parsing for http://localhost/register_insert.php

I am trying to send http post request from my register.component.ts page to register_insert.php page. But I am getting HttpErrorResponse error. Where I am doing wrong? please help

register.component.ts

`import { Component, OnInit } from ‘@angular/core’; import { HttpClient, HttpHeaders, HttpErrorResponse } from ‘@angular/common/http’;

@Component({ selector: ‘app-register’, templateUrl: ‘./register.component.html’, styleUrls: [‘./register.component.css’] }) export class RegisterComponent {

fullname : any; email : any; role : any; typee : any; password : any;

constructor(private httpClient: HttpClient) {

}

insertData() { this.httpClient.post( “http://localhost/register_insert.php”, { ‘fullname’:this.fullname, ‘email’:this.email, ‘role’:this.role, ‘typee’:this.typee, ‘password’:this.password,

      },



  ).subscribe((data) => {
      alert("inserted");
      console.log(data);
      this.fullname = null;
      this.email = null;
      this.role = null;
      this.typee = null;
      this.password = null;
  }, 
  (err:HttpErrorResponse)=>{
    console.log(err.message);
});

} } `

register_insert.php

`<?php

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: GET, POST, PUT, PATCH, POST, DELETE, OPTIONS");
    header('Access-Control-Max-Age: 86400');
    header("Access-Control-Expose-Headers: Content-Length, X-JSON");
    header("Access-Control-Allow-Headers: *");

include ‘dbcon.php’;

$data = json_decode(file_get_contents(“php://input”),true); if(count($data) > 0 ) {

$fullname = mysqli_real_escape_string($connect,$data['fullname']);
$email = mysqli_real_escape_string($connect, $data['email']);
$role = mysqli_real_escape_string($connect,$data['role']);
$type = mysqli_real_escape_string($connect, $data['typee']);
$password = mysqli_real_escape_string($connect,$data['password']);

$insert = "INSERT INTO tbl_user values ('','$fullname','$email','$role','$type','$password')";


if(mysqli_query($connect,$insert)){
  echo "Data inserted";
} else {
  echo "Data not inserted" . mysqli_error($connect);
}

} ?> `

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Http failure during parsing for http://localhost/register_insert.php

This is the error message

Apparently, @ericmartinezr has given you one of our canned responses and several 👎’s already.

@imguccho, I’d like to apologize for this unfriendly greeting to our community. I want to make sure that you know you’re welcome here.

The canned response is correct, though, we don’t handle support requests via the issue tracker. Here’s an explanation

I think you’ll have better luck on StackOverflow. Sorry for the trouble.

Hello, we reviewed this issue and determined that it doesn’t fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag angular.

If you are wondering why we don’t resolve support issues via the issue tracker, please check out this explanation.

Yes sir, its form data. what header I need to pass?

Sir, Go to the link below. This errors I am getting. https://ibb.co/3Mxnr8k

For the same reason, you request and response format is not json, it should be form data as this is an page…

I got u sir. But why I am getting this error?

SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:7679:51)

In case you are using Rest api which accepts abs send response in json it is not, In your case your trying to send data to a page, Angular http service by default sets the header to accept and send json data. Hope i answered your question.

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: “OK”, url: “http://localhost/register_insert.php”, ok: false, …} error: error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:7679:51) at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2781:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:53142:33) at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2780:60) at Zone.push…/node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2553:47) at ZoneTask.push…/node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2856:34) at invokeTask (http://localhost:4200/polyfills.js:4102:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:4139:21) text: "
Warning: include(dbcon.php): failed to open stream: No such file or directory in C:\xampp\htdocs\register_insert.php on line 9

Warning: include(): Failed opening ‘dbcon.php’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\register_insert.php on line 9
↵ " proto: Object headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} message: “Http failure during parsing for http://localhost/register_insert.php” name: “HttpErrorResponse” ok: false status: 200 statusText: “OK” url: “http://localhost/register_insert.php

Is it necessary to send headers from typescript file?

Angular by default sets the content type to application/json can you verify the content type accepted by your PHP code, as this is not a rest api, I think content type text or application/x-www-form-urlencoded should work.

What is the error, can you share it as well.