Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3456086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:41:36+00:00 2026-05-18T09:41:36+00:00

Here is my PHP code, it’s getting a listing of collections from mongodb $list

  • 0

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);
    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:41:36+00:00Added an answer on May 18, 2026 at 9:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is the input i am getting from my flash file process.php?Q2=898&Aa=Grade1&Tim=0%3A0%3A12&Q1=908&Bb=lkj&Q4=jhj&Q3=08&Cc=North%20America&Q0=1 and in
Here is my PHP code $xml= new SimpleXmlElement($rawxml); foreach($xml->children()->children() AS $key){ $id = $xml->{id};
here is the code <php? $id1 =1; $id2 = module 1 loaded; echo $var1=$id1,$var2=$id2;
Here's the code. Not much to it. <?php include("Spreadsheet/Excel/Writer.php"); $xls = new Spreadsheet_Excel_Writer(); $sheet
Here's the XML file i'm working on: <list> <activity>swimming</activity> <activity>running</activity> <activity>soccer</activity> </list> The index.php,
Here is my PHP code: echo '<br />1. '.$w_time_no; echo '<br />2. '.strtotime($w_time_no); echo
Here is my PHP Code $phpver = phpversion(); $useragent = (isset($_SERVER[HTTP_USER_AGENT]) ) ? $_SERVER[HTTP_USER_AGENT]
Here is my PHP code.. $input = zergling-light $output = str_replace('-', ' (', $input).);
i have a php code here and wondering how can this display what you
I have a php code here and I would like to create a "back"

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.