I am trying to post values from my formgoup to a php page to eventually save in a mysql database.When I attempt to POST, I keep getting the error response:
SyntaxError: Unexpected token a in JSON at position 41
at JSON.parse (<anonymous>)
at XMLHttpRequest.onLoad (http://localhost:8100/vendor.js:9017:51)
at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3505:35)
at Object.onInvokeTask (http://localhost:8100/vendor.js:75756:33)
at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3504:40)
at Zone.runTask (http://localhost:8100/polyfills.js:3273:51)
at ZoneTask.invokeTask [as invoke] (http://localhost:8100/polyfills.js:3586:38)
at invokeTask (http://localhost:8100/polyfills.js:4727:18)
at XMLHttpRequest.globalZoneAwareCallback (http://localhost:8100/polyfills.js:4764:25)
The code I am using in the ionic app is :
let body = {
fullname: this.slideOneForm.value['fullName'],
gender: this.slideOneForm.value['gender'],
age: this.slideOneForm.value['age'],
mstatus: this.slideOneForm.value['mstatus'],
flocation: this.slideOneForm.value['flocation'],
telno: this.slideOneForm.value['telno'],
email: this.slideOneForm.value['email'],
username: this.slideOneForm.value['username'],
password: this.slideOneForm.value['password'],
enterprise1: this.selected_enterprises[0],
enterprise2: this.selected_enterprises[1],
enterprise3: this.selected_enterprises[2],
farmstock1: this.slideTwoForm.value['farm_stock'][0],
farmstock2: this.slideTwoForm.value['farm_stock'][1],
farmstock3: this.slideTwoForm.value['farm_stock'][2],
farmharvest1: this.slideTwoForm.value['farm_harvest'][0],
farmharvest2: this.slideTwoForm.value['farm_harvest'][1],
farmharvest3: this.slideTwoForm.value['farm_harvest'][2],
harvestmeasure1: this.slideTwoForm.value['harvest_measure'][0],
harvestmeasure2: this.slideTwoForm.value['harvest_measure'][1],
harvestmeasure3: this.slideTwoForm.value['harvest_measure'][2],
land: this.slideTwoForm.value['land'],
farmgroup: this.slideTwoForm.value['farmgroup'],
seeds: this.slideTwoForm.value['seeds'],
acreagelseason: this.slideTwoForm.value['acreagelseason'],
acreagetseason: this.slideTwoForm.value['acreagetseason'],
mmnumber: this.slideThreeForm.value['mmnumber'],
bank: this.slideThreeForm.value['bank'],
sales: this.slideThreeForm.value['sales'],
tillage: this.slideThreeForm.value['tillage'],
financialservice: this.slideThreeForm.value['financialservice'],
registeredby: this.officername,
};
body = JSON.stringify(body);
this.postData(body);
postData(body){
//let type = 'application/json; charset=utf-8';
//let headers = new Headers({ 'Content-Type': type });
//let options = new RequestOptions({ headers: headers });
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'content-type':"application/x-www-form-urlencoded"
})
};
return this.http.post(this.server + "add_farmer.php", body, httpOptions)
.subscribe(async data =>
{
//
this.dataRegister = data;
});
}
The code I am using in the PHP page is:
header('Access-Control-Allow-Origin: http://localhost:8100 ');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Content-Type: application/json; charset=UTF-8');
include 'config.php';
$postjson = json_decode(file_get_contents('php://input'), true);
$username = mysqli_query($db_handle, "SELECT * FROM farmers WHERE username='" . $postjson['username'] . "'");
I have tried variations like
body = JSON.parse(JSON.stringify(body));
But it still returns the same type of error but usually about an "Unexpected Token at position 0"
Thanks
Sorted it. There was a syntax error in my sql request :)