I have no idea why I cannot get jquery to parse a JSON array from a PHP server side call.

Here is my jQuery

$.ajax({
        type:"POST",
        url:"/Home/Profile/cb_profile.php",
        data:{loadProfile: JSON.stringify(itemsToPost)},
        success: function(data){

            var returnedItem = $.parseJSON(data);           

        },
        error: function(e){
            console.log("We've had a error");
        },
    });

Here is my PHP

if(isset($_REQUEST['loadProfile']))
    {
        $connection = connectSQL();

        $userID = getID();

        $query = "SELECT * FROM users where usersID = $userID";

        $results = query($connection,$query);

        $row = $results->fetch_assoc();

        $firstName = "Steve";

        echo json_encode(array("firstName" => $firstName));

        disconnectSQL($connection);
    }

I get this error inside of firebug.

SyntaxError: JSON.parse: unexpected character

return window.JSON.parse( data );

Please help...

Re: Help with returning PHP JSON to jQuery $.ajax 80 80

What error message are you getting? Are you making it into the success section but just returnedItem is empty, or are you printing out that we've had an error to the JS console?

Re: Help with returning PHP JSON to jQuery $.ajax 80 80

I am making it to the success section and I get this error inside of firebug:

SyntaxError: JSON.parse: unexpected character

return window.JSON.parse( data );

If I console.log(data) I get: {"firstName":"Steve"}

If I add dataType:'json' parameter to the ajax call I go to "We've had an error".

Re: Help with returning PHP JSON to jQuery $.ajax 80 80

I commented out everything in my php file except for

$firstName = "Steve";
echo json_encode(array("firstName" => $firstName));

and it works. Now I have to track down what's causing the problem.

Re: Help with returning PHP JSON to jQuery $.ajax 80 80

I can't help you there because it seems you're calling a bunch of your own functions for the database. Those aren't standard PHP/MySQL functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.