I have a JSON object stored in a mongoDB collection. The object represents the positions of 5 x images, 5 y images, and a tictactoe board image.
On an interval, I send a request to a php file that responds with this object, and then I want to parse that object and move the pieces accordingly.
this is my request:
$.getJSON
(
"e4.php",
"",
function(data)
{
world = JSON.parse(data);
moveObjects(world);
}
);
but I get: JSON.parse: unexpected character
When I console.log data firebug gives me the right object so I know it's returning properly.
In e4.php:
$criteria = array("name" => "world");
$doc = $collection->findOne($criteria);
$conn->close();
print $doc['world'];
where conn is the connection, and collection is the collection I'm working in.
The database is updated in e3.php:
$encodedworld = $_REQUEST['data'];
$criteria = array("name" => "world");
$doc = $collection->findOne($criteria);
$doc['world'] = $encodedworld;
$collection->save($doc);
$conn->close();
print $encodedworld;
Any ideas? I'm stumped
Thanks in advance.
I have a JSON object stored in a mongoDB collection. The object represents the positions of 5 x images, 5 y images, and a tictactoe board image.
On an interval, I send a request to a php file that responds with this object, and then I want to parse that object and move the pieces accordingly.
this is my request:
$.getJSON
(
"e4.php",
"",
function(data)
{
world = JSON.parse(data);
moveObjects(world);
}
);
but I get: JSON.parse: unexpected character
When I console.log data firebug gives me the right object so I know it's returning properly.
In e4.php:
$criteria = array("name" => "world");
$doc = $collection->findOne($criteria);
$conn->close();
print $doc['world'];
where conn is the connection, and collection is the collection I'm working in.
The database is updated in e3.php:
$encodedworld = $_REQUEST['data'];
$criteria = array("name" => "world");
$doc = $collection->findOne($criteria);
$doc['world'] = $encodedworld;
$collection->save($doc);
$conn->close();
print $encodedworld;
Any ideas? I'm stumped
Thanks in advance.
Share Improve this question edited Mar 26, 2013 at 22:09 Cruncher asked Mar 26, 2013 at 22:02 CruncherCruncher 7,7961 gold badge34 silver badges70 bronze badges 2- Hard to tell if you don't show the JSON. How are you generating it? – Ruan Mendes Commented Mar 26, 2013 at 22:05
- Edited, although it appears the problem was actually in the original code that I posted. – Cruncher Commented Mar 26, 2013 at 22:13
1 Answer
Reset to default 7jQuery's getJSON
deserializes the JSON for you, so data
will be an object graph, not a string. From the documentation:
The
success
callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the$.parseJSON()
method.
So since data
has already been deserialized, you don't want or need to call JSON.parse
on it. Doing so will implicitly call toString
on data
, which will return either [object Object]
or [object Array]
, hence JSON.parse
not liking it as input. :-) Just use data
directly:
$.getJSON
(
"e4.php",
"",
function(world) // <=== Changed name of argument
{
moveObjects(world); // <=== Used it directly
}
);
Separately: Unless you declared world
somewhere you didn't show, your code was also falling prey to The Horror of Implicit Globals. You probably wanted to have var
in there. But with the change above, you don't need the variable at all, so...