I am creating an app with Angular 9 and Ionic 5 and I am very new at this so forgive me if I missed something obvious. The following is my App code:
import { HttpClient } from '@angular/common/http';
etc...
constructor(private http: HttpClient) { }
authenticate(phone: string, password: string) {
let frmData = new URLSearchParams();
// let frmData = new FormData(); //just to show that I have tried this also
const headeroptions = {
// 'enctype': 'multipart/form-frmData;', // when using FormData above
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
};
frmData.append('emailOrMobile', phone);
frmData.append('password', password);
console.log("login: " + phone + " " + password); //this shows the values correctly
console.log("frmData: " + frmData); //frmData: emailOrMobile=1234567890&password=111111
return this.http.post<any>('https://example.com/APP/login.php', {frmData}, {headers: headeroptions}
// {"emailOrMobile":phone, "password":password} //Also tried without creating the object beforehand
).subscribe(data => {
console.log("Subscribed Data: ");
console.log(data);
},
error => {
console.log('error: ' + error.error);
console.log('Name: ' + error.name);
console.log('Message: ' + error.message);
console.log('Status: ' + error.status);
});
}
I have also tried other versions where I have replaced .subscribe... etc. with:
.pipe(tap(this.setUserData.bind(this)));
but that hasn't worked either. There are a lot of solutions out there, but most deal with older versions of Angular and do not use HttpClient or they do and suggest subscribing to the observable - which I am already doing.
At the other end, the php code is straightforward:
$data = json_decode(file_get_contents("php://input"), true);
If I echo or try to use $data, it is an empty array that prints as {} or just a blank space (can't get it consistently to print '{}'). I even tried
count($data);
and that returns 0.

frmDatainstead of{frmData}?