Context:
Newbie here. I'm currently making a project using Angular 5 as front-end and PHP as back-end. I'm just trying to make a post method sending a string as parameter with an Angular Service and using that string as a parameter for my sql query in php.
Issue :
I managed without problem to send my string to my service. But then sending it using my post method produce this error :
ERROR {…} error: Object { error: SyntaxError, text: "array(1) {\n [\"{\"name\":\"Rick_Sanchez\"}\"]=>\n string(0) \"\"\n}\n
\nNotice: Undefined index: name in C:\xampp5.6\htdocs\compte-rendu-app\api\prtcptData\getPrtcptData.php on line 16
\nnull" }headers: Object { normalizedNames: Map, lazyUpdate: null, lazyInit: HttpHeaders/this.lazyInit() }
message: "Http failure during parsing for http://localhost:4200/compte-rendu-app/api/prtcptData/getPrtcptData.php"
name: "HttpErrorResponse"
ok: false status: 200
statusText: "OK"
url: "http://localhost:4200/compte-rendu-app/api/prtcptData/getPrtcptData.php"
So what i understand from it, is that there is an issue implying the JSON parsing of my parameter. But i used several ways of sending it and nothing seems to work. Otherwise the issue could be coming from my php. I used almost the same code for a get a method and it worked just fine.
My files :
prtcptData.service.ts :
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable()
export class PrtcptDataService {
urlGet = '/compte-rendu-app/api/prtcptData/getPrtcptData.php';
constructor(private http: HttpClient) {}
/* GET data from the selected name */
getPrtcptData(name: string): Observable<PrtcptData> {
return this.http.post<any>(this.urlGet, name, httpOptions);
}
}
getPrtcptData.php
<?php
session_start();
require('./../fonctions.php');
function formData($data){
if($data == null) {
return 'Missing Data';
}else{
return $data;
}
}
$post_name = $_POST['name'];
$bdd = connectDb("Crapo");
$cmdSQL = "SELECT ipt_id, ipt_organisme, ipt_prenom_nom,
ipt_fonction, ipt_mail, ipt_telephone
FROM INFOS_PARTCPT
WHERE ipt_prenom_nom = :post_name";
$stmt = $bdd->prepare($cmdSQL);
$stmt->bindParam(':post_name', $post_name, PDO::PARAM_STR);
$stmt->execute();
$list = array();
while($row = $stmt->fetchObject()){
$retour[] = array(
'ipt_id' => formData($row->ipt_id),
'ipt_organisme' => formData($row->ipt_organisme),
'ipt_prenom_nom' => formData($row->ipt_prenom_nom),
'ipt_fonction' => formData($row->ipt_fonction),
'ipt_mail' => formData($row->ipt_mail),
'ipt_telephone' => formData($row->ipt_telephone)
);
}
header('Content-Type: application/json');
if(empty($retour)) $retour = null;
echo json_encode($retour);
So i think the real question is : what is the right way to do my post method with my current setup ? I've been trying many solutions for several hours but i'm out of them right now. Can someone help me ?
Thank you