admin管理员组文章数量:1334335
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...
本文标签: javascriptJSONparse Unexpected characterStack Overflow
版权声明:本文标题:javascript - JSON.parse Unexpected character - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742368709a2461793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论