1

Here is my PHP code, it's getting a listing of collections from mongodb

$list = $db->dbname->listCollections();
$result = array();
$i=0;
foreach ($list as $thiscollection) {
    $result[$i++] = $thiscollection->getName();
}
echo json_encode( $result );

I do console.log in the callback and this is what I see.

["fruits", "dogs", "cars", "countries"]

The problem is that this is a string, not an array. I need to iterate through these values. How an I make this into a real object or get php to give me json rather than php array so I can use parseJSON on it.

Thanks.

js:

$.post('/ajax-database.php', function (data) {
    console.log($.parseJSON(data));
    $.each(data, function (key, value) {
        console.log(value);
    });
});
4
  • It is an array if the console print that out. Commented Nov 27, 2010 at 18:54
  • @timdream when isArray says it's not. When I try to run $.each on it i get every character, including the brackets and the quotes. Commented Nov 27, 2010 at 19:01
  • That's wired. Could you post your Javascript function? Maybe header('Content-Type: text/javascript'); before echo to make sure jQuery interpret the data as json? Commented Nov 27, 2010 at 19:03
  • @timdream adding "json" as datatype to the end of the post did the trick. I guess I don't understand how that works, I thought parseJSON did the same thing? Blerg. Commented Nov 27, 2010 at 19:18

2 Answers 2

3

I see you are using jquery, if you want data to come back to you as a json object you need to do 1 of 2 things.

  1. add header("Content-Type: application/json") to your php file, this will tell jquery to convert it to a json object instead of as text

  2. Add a forth parameter to your $.post,

$.post('/ajax-database.php', function (data) {
    console.log($.parseJSON(data));
    $.each(data, function (key, value) {
        console.log(value);
    });
}, "json");

that will tell jquery to call your error handler if its NOT json, like if your php code fails and outputs html instead. You really should use $.ajax, i have no idea why anyone uses $.post, you can't do ANY meaningful error handling.

Sign up to request clarification or add additional context in comments.

2 Comments

If a request with jQuery.post() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method. - api.jquery.com/jQuery.post
Yes I know, I just consider it bad practice to globally handle errors in that respect, as each different script could and probably will error differently.
0

JSON is strings. If you want to be able to iterate over it then you need to decode it.

8 Comments

yeah but the problem is that it's not json. it it's got square brackets.
Yes, it is. The top value is not required to be an object.
@Mark: remember [] just mean Array, and all array's in javascript ARE objects, they have prototypes.
Specifically, the top value in JSON can be any of the things in the value construct here: json.org
The question was asking how to decode the string I got. I can run parseJSON on the string, and the values inside the fake array became json objects, but I still couldn't iterate over the entire array. The fake array became a real object after I set the datatype. I'm pretty confused by this answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.