0

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

Edited by singularity~: Forgot a few things

2
Contributors
4
Replies
12
Views
3 Years
Discussion Span
Last Post by Dani
0

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?

0

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

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.