SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

IONIC -5
access-provider.ts

export class AccessProviders{
    server:string='http://localhost/api/';
    constructor(private http:HttpClient
        ) { }

        postData(body,file){
            let headers=new HttpHeaders({
            'Content-Type':'application/json; charset=UTF-8'
            });
            let options= { 
                headers : headers
            }
            return this.http.post(this.server + file , JSON.stringify(body),options)
            .timeout(590000)
            .map(res=>res);
  
        }
       
   }

register.ts

async tryRegister(){
        if(this.your_name==""){
        this.presentToast('your name is required');
        }else if(this.email_address==""){
          this.presentToast('Email is required');
        }else if(this.password==""){
        this.presentToast('password is required');
        }else if(this.confirm_pass!=this.password){
        this.presentToast('Password not match');
        }else{
        this.disabledButton=true;
        const loader = await this.load.create({
        message:'Please Wait.....',
    });
    loader.present();
  return new Promise(resolve=>{
    let body = {
    aksi: 'process_register',
    your_name: this.your_name,
    email_address: this.email_address,
    password: this.password,
    confirm_pass: this.confirm_pass
    }
   

this.accsPrvds.postData(body,'process_api.php').subscribe((res:any)=>{
console.log("hello");
if(res.success == 'true'){
loader.dismiss();
this.disabledButton=false;
this.presentToast(res.msg);
this.router.navigate(['/login']);
}else {
  loader.dismiss();
  this.disabledButton=false;
  this.presentToast(res.msg); 

}

},(err)=>{
  loader.dismiss();
  this.disabledButton=false;
  this.presentAlert('Timeout');
});

    });
  }



  }

process_api.php

<?php

header('Access-Control-Allow-Orgin: *');

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin,Content-Type,Authorization,Accept,X-Requested-With,x-xsrf-token");
header("Content-Type: application/json;charset=utf-8");
include "config.php";
$postjson=json_decode(file_get_contents('php://input'),true);

if($postjson['aksi']=="process_register"){
  
          $checkmail=mysqli_fetch_array(mysqli_query($mysqli,"SELECT email_address FROM register WHERE email_address='$postjson[email_address]'"));

          if($checkmail['email_address']==$postjson['email_address']){
            $result=json_encode(array('success'=>false,'msg'=>'Email Already Registered'));
          }else{

                $password=md5($postjson['password']);

                $insert = mysqli_query($mysqli,"INSERT INTO register SET 
                your_name ='$postjson[your_name]',
                email_address ='$postjson[email_address]', 
                password ='$password', 
                confirm_pass ='$postjson[confirm_pass]'
                
                ");
                if($insert){
                    $result=json_encode(array('success'=>true,'msg'=>'Register Successfully'));
                }else{
                    $result=json_encode(array('success'=>false,'msg'=>'Register error'));
                }

          }
          echo $result;
}
?>

config.php

<?php
define('db_name','ionic_db');
define('db_user','root');
define('db_pass','');
define('db_host','localhost');
$mysqli = new mysqli(db_host,db_user,db_pass,db_name);
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";

?>

Json variable not passing properly because i am getting this output

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Connected successfully{“success”:true,“msg”:“Register Successfully”}

The data stored database but return to client page register.ts this.accsPrvds.postData(body,‘process_api.php’).subscribe((res:any)=>{ } -->If condition not work (client side notification not work)

Ok thank you for your reply ,i checked but it shows similar error and user details not register to the database,extra one warning( Illegal string offset ‘aksi’ in /Applications/XAMPP/xamppfiles/htdocs/api/process_api.php on line)

I’m confused by your question, because the end of your post doesn’t seem to be consistent with the error in the thread title. The thread title is caused by your server process not returning JSON. I don’t do PHP, so somebody else is going to have to deal with why that is happening, but some other notes:

  • Never use MD5 for anything. It’s basically ROT13 at this point. For password hashing, use an algorithm designed for it, such as bcrypt.
  • You almost never need to be declaring Content-Type in an Angular app, and certainly not here. Let Angular do it.
  • Ditto with manually stringifying the body - not needed and actually harmful here
  • Get rid of map(res => res) - it’s pointless
  • Always put proper types on all parameters and function return values - it will make your code much easier to read and debug
  • Conversely, you don’t need to type properties like server that are initialized inline to a knowable type

Thank you for your reponse…
Json variable not passing properly because i am getting this output

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Connected successfully{“success”:true,“msg”:“Register Successfully”}

The data stored database but return to client page register.ts this.accsPrvds.postData(body,‘process_api.php’).subscribe((res:any)=>{ } -->If condition not work (client side notification not work)

Lose this. It’s breaking the JSON.

Thank you so much :grinning:…The error was gone… :partying_face: :partying_face: :partying_face: Its very helpful for me… :grinning: