I have a PHP function that simply calls the database for an array of results and then outputs it as a JSON object like so:
PHP:
function getSubscriptionDetails(){
$subscription = $this->client_model->getSubscription($this->user->info->ID);
header('Content-Type: application/json');
echo json_encode($subscription);
}
Result:
{
maxDenomination: "100",
startDate: "2018-01-19 19:44:41",
endDate: "2018-02-19 19:44:41",
subscriptionID: "8",
packageID: "1",
packageName: "Silver"
}
My goal is to be able to use this in my javascript AJAX call but when I try and run JSON.parse
on it, it says its not valid.
From what I gathered, valid JSON is supposed to have quoted key names as well. I glanced through json_encode
but couldn't see anything that enabled this.
Am I missing something here? I am not manually creating the array so I can't quote them myself, was hoping it was just a option I could pass with the encoding.
your result a json object you dnt need to parse it if it is a json string you need to prase it
to get the value of maxDenomination try the below
var a = {
maxDenomination: "100",
startDate: "2018-01-19 19:44:41",
endDate: "2018-02-19 19:44:41",
subscriptionID: "8",
packageID: "1",
packageName: "Silver"
}
console.log(a.maxDenomination);