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

  • SEARCH
  • Home
  • 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 7752757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:52:48+00:00 2026-06-01T11:52:48+00:00

Goal: json_encode a PHP object that has private properties Send the encoded object as

  • 0

Goal:

  1. json_encode a PHP object that has private properties
  2. Send the encoded object as a datastring through Low Level AJAX using jQuery
  3. json_decode the PHP object in the AJAX URL to which the request is sent
  4. win

Problem:

On step 3, json_last_error is returning 3 (JSON_ERROR_CTRL_CHAR Control character error, possibly incorrectly encoded)

The Class:

class Stream {
    private $limit;
    private $type;
    private $sort;
    private $offset=0;
    private $userID;
    private $catID;
    private $content = array();
    private $num_posts;

    function __construct(){
        $a = func_get_args();
        $i = func_num_args();
        if (method_exists($this,$f='__construct'.$i)) {
            call_user_func_array(array($this,$f),$a);
        }
    }

    function __construct5($limit, $type, $sort, $userID, $catID){
        $this->limit = $limit;
        $this->type = $type;
        $this->sort = $sort;
        $this->userID = $userID;
        $this->catID = $catID;
        //$this->num_posts = $this->retrieveTotal();

        //$this->setContent(); 
    }

    function __get($name) {
        if(isset($this->$name)){
            return $this->$name;
        }
    }

        public function encodeJSON(){
        foreach ($this as $key => $value){
            if($key != 'content'){
                $json->$key = $value;
            }
        }
        return json_encode($json);
    }

    public function decodeJSON($json_str){
        $json = json_decode($json_str, true);
        echo '<br>error: '.json_last_error();

        foreach ($json as $key => $value){
            $this->$key = $value;
        }
    }
}

//create the object to be encoded
$strm = new Stream(5, 'toc', ' ', 1, ' ');

/*this test works

$d=$strm->encodeJSON();

$st = new Stream();
$st->decodeJSON($d);
*/

The AJAX function:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    //load more posts
    $("#active").live("click", function() {
    var stream= '<?= $strm->encodeJSON();?>'; 
        var dataString = 'stream='+stream;
        var request = $.ajax({  
            type: "POST",  
            url: "ajax/loadmore.php",  
            data: dataString,
            beforeSend:function(data){
                $('.load-more').html('<img src="ajax/loader.gif" alt="Loading..." />');
            },
            success: function(html) {
                $('#active').remove();
                $('#stream').append(html);
            }
        });

        //ajax error reporting
        request.fail(function(jqXHR, textStatus) {
            $('#active').html(textStatus);
        });
    });
</script>

<a class='load-more' id='active'>load more posts</a>

The AJAX request (loadmore.php):

require_once'../../classes/stream.class.php';
$strm = new Stream();
$strm->decodeJSON($_POST['stream']);

What I have tried:

This snippet of code

$d=$strm->encodeJSON();

$st = new Stream();
$st->decodeJSON($d);

works fine. That would lead me to believe that AJAX is interfering with the decoding.

I have also tried changing $json = json_decode($json_str, true); to $json = json_decode(utf8_encode($json_str), true); and nothing changes.

NOTE: suggesting that I make the class properties public is NOT a solution

EDIT: when I echo the string, {
"limit": "5",
"type": "toc",
"sort": " ",
"offset": "0",
"userID": "3",
"catID": " ",
"num_posts": "2"
}
being sent to decodeJSON it tests as valid

This screenshot shows the arg $json_str that is being sent to decodeJSON($json_str) and the error code.
json_str param and error

  • 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-06-01T11:52:50+00:00Added an answer on June 1, 2026 at 11:52 am

    After much trial and error I figured out the issue. When I was instantiating the Stream objectthat was to be encoded, instead of using 1 i was using $userID which was being cast as a string and messing with the URI encoding

    stream=%7B%22limit%22%3A%225%22%2C%22type%22%3A%22toc%22%2C%22sort%22%3A%22%20%22%2C%22offset%22%3A%220%22%2C%22userID%22%3A%223%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%22%2C%22catID%22%3A%22%20%22%2C%22num_posts%22%3A%222%22%7D

    I’m not really sure WHY it behaves that way, but the solution is casting the $userID as an integer. so:

    $strm = new Stream(5, 'toc', ' ', (int)$userID, ' ');
    

    The URI encoding changes to:

    stream=%7B%22limit%22%3A%225%22%2C%22type%22%3A%22toc%22%2C%22sort%22%3A%22%20%22%2C%22offset %22%3A%220%22%2C%22userID%22%3A%223%22%2C%22catID%22%3A%22%20%22%2C%22num_posts%22%3A%222%22%7D

    and json_decode return an array.

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

Sidebar

Related Questions

My Basic Goal A Rails app that can load new content via Ajax while
I'm using jQuery's $.ajax method to call getjson.php which returns a JSON object using
Goal: Post Image using RestTemplate Currently using a variation of this MultiValueMap<String, Object> parts
I'm evaluating PHP frameworks for several upcoming projects, with the goal of using the
My original goal was to get a php file to execute on a button
My php script sends back a JSON encoded string. I'm just lost on how
Goal: Expose a simple WCF service that would take as a parameter a string
When using PHP's json_encode to encode an array as a JSON string, is there
My goal is to build a standalone RESTful Rails 3 service that communicates with
I have a PHP object like so: Array ( [Apples] => stdClass Object (

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.