SyntaxError: Unexpected token S in JSON at position 0


#1

i’m having this error i couldn’t figure out the problem please help me

SyntaxError: Unexpected token S in JSON at position 0
at JSON.parse (<anonymous>)

my-service.ts

UpdateOffreLocation(dataOffres)
 {
  var url = 'http://localhost/PFEBACKEND/manage-location.php?
 key=update&date_expiration='+dataOffres.date_expiration+   '&id_chef='+dataOffres.id_chef+'&id_vehicule='+dataOffres.id_vehicule+'&id_chauffeur='+dataOffres.id_chauffeur+
  '&adresse='+dataOffres.adresse+'&id_offre='+dataOffres.id_offre;
 var response = this.http.get(url).map(res => res.json()); 
  return response;
 }

page.ts

 dataOffres = {date_expiration:'',id_chef:null,id_vehicule:null,id_chauffeur:null,adresse:'',id_offre:null};

this.dataOffres.adresse   	= this.formLocation.controls["adresse"].value;
this.dataOffres.date_expiration 	= this.formLocation.controls["date_expiration"].value;
this.dataOffres.id_vehicule    	= this.formLocation.controls["id_vehicule"].value;
this.dataOffres.id_chauffeur   	= this.formLocation.controls["id_chauffeur"].value;
this.dataOffres.id_chef=this.id;
this.dataOffres.id_offre=this.offre.id_offre;

UpdateLocation()
{ 
  this.data.UpdateOffreLocation(this.dataOffres).subscribe(
    data => {       
      console.log(this.dataOffres.id_chef);
      console.log(this.dataOffres.date_expiration);
        if(data.result =="succees")
        {
            this.navCtrl.popTo(OffresPage);
            this.loader.dismiss();
        }else 
         { this.showPopup("Désolé","aucune champs a été modifé !!");
            }
    },
    err => {
      console.log(err);
       this.loader.dismiss();
    },
    () => console.log('modification terminée')
);
}

php-backend function

(..)
 case "update":

     // Sanitise URL supplied values
     $id_offre  =  filter_var($_REQUEST['id_offre'], FILTER_SANITIZE_NUMBER_INT);
     $date_expiration= ($_REQUEST['date_expiration']);
     $id_vehicule = filter_var($_REQUEST['id_vehicule'], FILTER_SANITIZE_NUMBER_INT);
     $id_chauffeur = filter_var($_REQUEST['id_chauffeur'], FILTER_SANITIZE_NUMBER_INT);
     $adresse = filter_var($_REQUEST['adresse'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
    

     // Attempt to run PDO prepared statement
     try {
        $sql="UPDATE `offre` SET `date_expiration`=:date_expiration,`id_vehicule`=:id_vehicule,`id_chauffeur`=:id_chauffeur WHERE id_offre = :id_offre";
         $stmt 	=	$pdo->prepare($sql);
         $stmt->bindParam(':date_expiration',$date_expiration, PDO::PARAM_STR);
         $stmt->bindParam(':id_chauffeur', $id_chauffeur, PDO::PARAM_INT);
         $stmt->bindParam(':id_vehicule', $id_vehicule, PDO::PARAM_INT);
         $stmt->execute();

        $sql1="UPDATE `offre_location` SET `adresse`=:adresse WHERE id_offre_location = :id_offre"; 
        $stmt1    =  $pdo->prepare($sql1);
        $stmt1->bindParam(':adresse', $adresse, PDO::PARAM_STR);
         $result = $stmt1->execute();
       
                  if($result){
              $data["result"] = "succees";
                echo json_encode($data);   
               }else{ 
                $data["result"] = "ressayer";
                echo json_encode($data); 
                 }
                      
     }
(..)

#2

Your server is apparently not returning JSON, so look at what is going over the wire with Chrome Developer Tools’ Network tab.

Incidentally, your API breaks the cardinal rule of REST. GET must never modify state; it can only be used for reading data. You must switch to POST or PUT to modify state on the server.


#3

yess when i switched to post it worked :smiley: thanxxxx