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...

Recommended Answers

All 4 Replies

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?

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".

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.

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.