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 ..
Your problem is that you’re outputting multiple JSON objects together.
This is because each
Postobject in your PHP code outputs a single block of JSON. You’re creating an array ofPostobjects, 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
Postobjects output an array of their JSON objects, then you need to write the JSON code for that as well.You can’t
json_encodethe 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 eachPostobject output.You could do it like this:
instead of your
foreach()loop.