I am currently working on a PHP/JavaScript/jQuery project where PHP returns some JSON encoded data to a web page via an ajax request.
The JSON data contains multiple objects and an array but I can't see how to get process the data.
Below is how the JSON is created in PHP
$result = mysql_query($query);
if ($result)
{
$data = array();
while ($myrow = mysql_fetch_array($result))
{
$data[] = $myrow['VersionName'];
}
$returnArray["RESULT"] = HelperClass::generateResponseArray(HTTPResponseCodes::OK, true, null, false);
$returnArray["DATA"] = $data;
return json_encode($returnArray);
}
The generateResponseArray function looks like the following:
public static function generateResponseArray($httpStatusCode, $wasActionSuccessful, $message,
$jsonEncodeArray = true)
{
$response = array();
$response["HTTPStatusCode"] = $httpStatusCode;
$response["actionSuccessful"] = $wasActionSuccessful;
$response["Message"] = $message;
if ($jsonEncodeArray)
{
return json_encode($response);
}
else
{
return $response;
}
}
Below is how I am trying to decode the JSON via JavaScript.
function getVersions()
{
$.post("../Middleware/CrashManagement.php",
{
type: "GetVersions",
appID: getParameterValue("id")
},
function (result)
{
var obj = JSON.parse(result);
var resultData = JSON.parse(obj.RESULT);
if (resultData.actionSuccessful === true)
{
alert("hello");
}
}
)
}
Chrome's not giving me any pointers as to where the problem is, it just says unexpected end of input at my html start tag for some reason, but it something to do with this function as if I remove chrome no longer reports this error.
getJSONrather thanpost? Also, I'm pretty sure all jQuery's Ajax functions return parsed data so you should just be able toresult.actionSuccessfulto get at the value once the data has been returned.