0

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.

1
  • Any reason you're not using getJSON rather than post? Also, I'm pretty sure all jQuery's Ajax functions return parsed data so you should just be able to result.actionSuccessful to get at the value once the data has been returned. Commented Dec 5, 2014 at 10:30

1 Answer 1

1

I think the PHP stuff is ok.

But you are parsing the returned data twice and that is not necessary.

function getVersions() {
    $.post("../Middleware/CrashManagement.php",
        { type: "GetVersions", appID: getParameterValue("id") },
        function (result) {
           // parse returned json string into a json object
           var obj = $.parseJSON(result);

           // No need to reparse this as its already done above
           //var resultData = JSON.parse(obj.RESULT);

           var resultData = obj.RESULT;
           if (resultData.actionSuccessful === true) {
               alert("hello");
           }
        })
}

If your prefered browser is Chrome, can I suggest that you get familiar with the Chrome javascript debugger

This would have shown you where you were going wrong in a second.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was thinking you'd have to separately decode each object/array contained within the JSON. I do know about the Chrome debugger but it didn't give any error apart from what I had put in my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.