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 5959841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:44:29+00:00 2026-05-22T18:44:29+00:00

Well I have a problem reading the JSON data in jQuery, which is outputted

  • 0

Well I have a problem reading the JSON data in jQuery, which is outputted by PHP,
I´m alreaddy using json_encode();

<?php
require_once 'Post_db.php'; // a Class for doing MySQL work

/* the Post class */    
class Post {

/* An array that stores the post item data: */

private $pdata;

/* The constructor */
public function __construct($postData){
if(is_array($postData))
$this->pdata= $postData;
}



public function __toString(){
// The string that is beeing returned
// is outputed by the echo statement

$toBeJsoned=$this->pdata;
$toBeSent=json_encode($toBeJsoned);
return  $toBeSent;  
}

//read the Posts
public static function readPosts(){
$query = new Post_db("SELECT * FROM pmessage
                 ORDER BY p_id DESC
                 ");
$posts = array();
while($row = $query->fetchRow()){
$posts [] = new Post($row);
}
foreach($posts as $item){

echo $item;
}
}

Now the Jquery :

This code is reading , but this is only a $.get method. And it returnes myPosts as 1 array with a about 5000 elmenets but there are only about 50 Posts, so it is getting each charachter from this string as an array element.

function readMyPosts(){
        $.get("phpFileThatCalltheFunctionsToRead.php",
                      {"action": "autoRead"},
                      function(myPosts){        
            //a ul element
            $("#plist").html("<li>"+myPosts+"</li>");       

               });
                 }
    readMyFacts();

When I try with $.ajax or $.getJSON, I never reach the success function, actully nothing happenes

  function ReadNewestFacts(){
        $.ajax({
        url:"phpFileThatCalltheFunctionsToRead.php", 
        data:"action=autoRead_main",
        dataType:"json",
        success: function(json){
            //do something with the data 
            alert(json);


            }});
      }
   //run this function
        ReadNewestFacts();

The phpFileThatCalltheFunctionsToRead.php file looks like this:

<?php

session_start();
require_once "post.class.php";


 if(isset($thisUid)){
$thisUid= $_GET['thisUid'];
}

if(isset($action)){
$action = $_GET['action'];
}

try{
    switch($_GET['action'])
    {
        case 'autoRead_main':
            Post::readPosts();
            break;      

        case 'autoRead':
            Post::readMyPosts($_GET['thisUid']);
            break;
            case ' more cases
               ..
                  .
                .
              .'




    }

}
catch(Exception $e){
    // echo $e->getMessage();
    die("0");
}

All I was able to recive is

{"p_id":"1","p_text":"blabla","p_adder":"1"}{"p_id":"2","p_text":"blabla2","p_adder":"1"}{"p_id":"3","p_text":"more blabla","p_adder":"2"}{"p_id":"4","p_text":"more and more blabla","p_adder":"1"}{}....

The JSON formatting seems to be working correctly !?,, but I think the Jquery not beeing able to read the JSON String from PHP ? I´m really not sure here 🙁 ..

I´m not beeing able to access the JSON data on the jQuery side.. I tried many methods found here on StacKover.. but I think im missing something ..

  • 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-22T18:44:30+00:00Added an answer on May 22, 2026 at 6:44 pm

    Your problem is that you’re outputting multiple JSON objects together.

    This is because each Post object in your PHP code outputs a single block of JSON. You’re creating an array of Post objects, and so you’re getting multiple blocks of JSON.

    Each block of JSON you’re generating is valid, but simply concatenating them together is not valid JSON.

    If you want to have your Post objects output an array of their JSON objects, then you need to write the JSON code for that as well.

    You can’t json_encode the whole thing, because it’s already encoded, so the easist way to fix it in your code is just to print [ and ] at either end of your output, and a comma between each Post object output.

    You could do it like this:

    echo '['.implode(',',$posts).']';
    

    instead of your foreach() loop.

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

Sidebar

Related Questions

Hi friends hope all r doing well. I have a problem while reading xml
Hi friends hope all r doing well. I have a problem while reading xml
Well here's my problem I have three tables; regions, countries, states. Countries can be
We have an application (well, 3) which uses Oracle 9i, however this is being
I have been reading this question and doesn't get the problem it encounters. why
well i have this messages table with sample values like these: msg_id recipient_id read
Well I have a videos website and a few of its tables are: tags
Well I have this MySQL stored procedure that I wrote and if I run
I have several input and option elements on my page, each (well almost) have
Well basically I have this script that takes a long time to execute and

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.