json#u解码+;ajax+;私有属性=灾难[错误:JSON\u error\u CTRL\u CHAR] 目标: json\u encode具有私有属性的PHP对象 使用jQuery将编码的对象作为数据字符串发送 json\u decode请求发送到的AJAX URL中的PHP对象 赢 问题:
在步骤3中,返回3(json#u解码+;ajax+;私有属性=灾难[错误:JSON\u error\u CTRL\u CHAR] 目标: json\u encode具有私有属性的PHP对象 使用jQuery将编码的对象作为数据字符串发送 json\u decode请求发送到的AJAX URL中的PHP对象 赢 问题:,php,jquery,ajax,json,Php,Jquery,Ajax,Json,在步骤3中,返回3(JSON\u ERROR\u CTRL\u CHAR控制字符错误,可能编码错误) 班级: 我所尝试的: 这段代码 $d=$strm->encodeJSON(); $st = new Stream(); $st->decodeJSON($d); 很好。这会让我相信AJAX正在干扰解码 我还尝试过更改$json=json\u decode($json\u str,true)到$json=json\u解码(utf8\u编码($json\u str),true)并且没
JSON\u ERROR\u CTRL\u CHAR控制字符错误,可能编码错误
)
班级:
我所尝试的:
这段代码
$d=$strm->encodeJSON();
$st = new Stream();
$st->decodeJSON($d);
很好。这会让我相信AJAX正在干扰解码
我还尝试过更改$json=json\u decode($json\u str,true)
到$json=json\u解码(utf8\u编码($json\u str),true)代码>并且没有任何更改
注意:建议将类属性公开不是解决方案
编辑:当我回显字符串时,{
“限制”:“5”,
“类型”:“toc”,
“排序”:“,
“偏移量”:“0”,
“用户ID”:“3”,
“catID”:“,
“帖子数量”:“2”
}
被发送到decodeJSON,它测试为
此屏幕截图显示发送到decodeJSON($json_str)的arg$json_str和错误代码。
JSON仅支持字符串的UTF-8。因此,在进行json_编码之前,您必须用UTF-8对所有字符串进行编码。这里发生的事情(我不理解你所有的代码)是,你有一个不带UTF8的字符串,它看起来像json_编码的“控制字符” JSON只支持字符串的UTF-8。因此,在进行json_编码之前,您必须用UTF-8对所有字符串进行编码。这里发生的事情(我不理解你所有的代码)是,你有一个不带UTF8的字符串,它看起来像json_编码的“控制字符” JSON\u错误\u CTRL\u CHAR的原因
它返回JSON\u ERROR\u CTRL\u CHAR
的原因不是因为字符编码(即utf8或iso)。当数据编码错误且生成的字符串不是有效的JSON时,它会遇到此错误。如果混合使用单引号“
和双引号”
也可能会干扰编码过程,保持一致性很重要
话虽如此,
它很可能返回错误,因为您首先没有发送任何实际数据。您正在通过请求发送一个空字符串。嗯,不是真的,您实际上是在发送字符串
<?= $strm->encodeJSON();?>
它应该能正常工作
混杂的
通常,当通过AJAX发送数据时,应该使用它来转义任何特殊字符
data:“stream=“+encodeURIComponent(stream),
$json=json\u decode(urldecode($json\u str),true);
.live
已取消分级
从1.7开始,该函数已被弃用,您现在应该使用该函数附加事件处理程序。在您的情况下,最好只使用速记函数
链接必须有href
stream.php
<?php
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(urldecode($json_str), true);
foreach ($json as $key => $value){
$this->$key = $value;
}
}
}
//create the object to be encoded
$strm = new Stream(5, 'toc', ' ', 1, ' ');
?>
loadmore.php
<?php
include_once('stream.php');
$strm = new Stream();
$strm->decodeJSON($_POST['stream']);
//Output a private property
echo $strm->__get("type");
?>
JSON\u错误的原因\u CTRL\u CHAR
它返回JSON\u ERROR\u CTRL\u CHAR
的原因不是因为字符编码(即utf8或iso)。当数据编码错误且生成的字符串不是有效的JSON时,它会遇到此错误。如果您混合使用单引号”
和双引号“
它也可能会干扰编码过程,保持一致性很重要
话虽如此,它很可能返回错误,因为您首先没有发送任何实际数据。您正在通过请求发送一个空字符串。嗯,不是真的,你实际上是在发送字符串
<?= $strm->encodeJSON();?>
它应该能正常工作
混杂的
通常,当通过AJAX发送数据时,应该使用它来转义任何特殊字符
data:“stream=“+encodeURIComponent(stream),
$json=json\u decode(urldecode($json\u str),true);
.live
已取消分级
该函数从1.7开始就不推荐使用,现在应该使用该函数附加事件处理程序。在你的情况下,你最好只使用速记功能
链接必须有href
stream.php
<?php
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(urldecode($json_str), true);
foreach ($json as $key => $value){
$this->$key = $value;
}
}
}
//create the object to be encoded
$strm = new Stream(5, 'toc', ' ', 1, ' ');
?>
loadmore.php
<?php
include_once('stream.php');
$strm = new Stream();
$strm->decodeJSON($_POST['stream']);
//Output a private property
echo $strm->__get("type");
?>
经过多次尝试和错误,我发现了这个问题。当我实例化要编码的
流对象
时,我使用的不是1
,而是$userID
,它被转换为字符串并与URI编码混淆
stream=%7B%22limit%22%3A%225%22%2C%22type%22%3A%22toc%22%2C%22%20%2C%22offset%22%3A%220%22%2C%22userID%22%3A%223%00%00%00%00%00%00%00%00%00%22%2C%22catID%22%3A%20%22%2C%22%222%7D
我不确定它为什么会这样,但解决方案是将$userID转换为整数。因此:
$strm = new Stream(5, 'toc', ' ', (int)$userID, ' ');
URI编码更改为:
stream=%7B%22limit%22%3A%225%22%2C%22type%22%3A%22toc%22%2C%22sort%22%3A%22%22%2C%22offset%22%3A%220%2C%22userID%22%3A%223%22%2C%22catID%22%3A%22%22%22%22%2C%22num posts%22%3A%22%7D
json_decode返回一个数组。经过多次尝试和错误,我发现了这个问题。当我实例化要编码的
流对象
时,我使用的不是1
,而是$userID
,它被转换为字符串并与URI编码混淆
stream=%7B%22limit%22%3A%225%22%2C%22type%22%3A%22toc%22%2C%22%20%2C%22offset%22%3A%220%22%2C%22userID%22%3A%223%00%00%00%00%00%00%00%00%00%22%2C%22catID%22%3A%20%22%2C%22%222%7D
我不确定它为什么会这样,但解决方案是将$userID转换为整数。因此:
$strm = new Stream(5, 'toc', ' ', (int)$userID, ' ');
<?php
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(urldecode($json_str), true);
foreach ($json as $key => $value){
$this->$key = $value;
}
}
}
//create the object to be encoded
$strm = new Stream(5, 'toc', ' ', 1, ' ');
?>
<?php
include_once('stream.php');
$strm = new Stream();
$strm->decodeJSON($_POST['stream']);
//Output a private property
echo $strm->__get("type");
?>
$strm = new Stream(5, 'toc', ' ', (int)$userID, ' ');