Home > Backend Development > PHP Tutorial > 对象序列化传至后台, php 无法还原对象

对象序列化传至后台, php 无法还原对象

WBOY
Release: 2016-06-23 13:49:44
Original
880 people have browsed it

前台页面把一个对象序列化以后提交到后台 用json_decode 还原不出对象了

function build_exitbox (){ // builid 构建编辑用的弹出窗口	var obj1={title:"合同资料",						fields:[							{display:"id",fields_name:"hetong_id",input_type:"text", disabled:true},							{display:"合同字号",fields_name:"hetong_zihao",input_type:"text", disabled:true},							{display:"备案号",fields_name:"beianhao",input_type:"text", disabled:false},							{display:"签约日期",fields_name:"qianyue_date",input_type:"text",class:"Wdate", disabled:false},							{display:"总加工费",fields_name:"t_jiagongfei",input_type:"text", disabled:false},							{display:"总出口值",fields_name:"t_chukou_zhi",input_type:"text", disabled:false},							{display:"总原料值",fields_name:"t_yuanliao",input_type:"text", disabled:false}],		};	var field_json=JSON.stringify(obj1);			open_pop();		$("#light").html(loading_html_str);		$("#light").load("bulid_exitbox.php",{cur_table_name:cur_table_name,cur_act_no:cur_act_type,cur_rec_id:cur_rec_id,field_obj:field_json}, function(){		});}
Copy after login


后台代码 "bulid_exitbox.php",
<?echo $field_obj;var_dump(json_decode($field_obj,true));?>
Copy after login


结果 后台接受的字串为
{\"title\":\"合同资料\",\"fields\":[{\"display\":\"id\",\"fields_name\":\"hetong_id\",\"input_type\":\"text\",\"disabled\":true},{\"display\":\"合同字号\",\"fields_name\":\"hetong_zihao\",\"input_type\":\"text\",\"disabled\":true},{\"display\":\"备案号\",\"fields_name\":\"beianhao\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"签约日期\",\"fields_name\":\"qianyue_date\",\"input_type\":\"text\",\"class\":\"Wdate\",\"disabled\":false},{\"display\":\"总加工费\",\"fields_name\":\"t_jiagongfei\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"总出口值\",\"fields_name\":\"t_chukou_zhi\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"总原料值\",\"fields_name\":\"t_yuanliao\",\"input_type\":\"text\",\"disabled\":false}]}
Copy after login


var_dump(json_decode($field_obj))  输出为null


回复讨论(解决方案)

测试了一下, 前台生成的  field_json 的值是这样的

{"title":"合同资料","fields":[{"display":"id","fields_name":"hetong_id","input_type":"text","disabled":true},{"display":"合同字号","fields_name":"hetong_zihao","input_type":"text","disabled":true},{"display":"备案号","fields_name":"beianhao","input_type":"text","disabled":false},{"display":"签约日期","fields_name":"qianyue_date","input_type":"text","class":"Wdate","disabled":false},{"display":"总加工费","fields_name":"t_jiagongfei","input_type":"text","disabled":false},{"display":"总出口值","fields_name":"t_chukou_zhi","input_type":"text","disabled":false},{"display":"总原料值","fields_name":"t_yuanliao","input_type":"text","disabled":false}]}

后台取得的值 是这样的
{\"title\":\"合同资料\",\"fields\":[{\"display\":\"id\",\"fields_name\":\"hetong_id\",\"input_type\":\"text\",\"disabled\":true},{\"display\":\"合同字号\",\"fields_name\":\"hetong_zihao\",\"input_type\":\"text\",\"disabled\":true},{\"display\":\"备案号\",\"fields_name\":\"beianhao\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"签约日期\",\"fields_name\":\"qianyue_date\",\"input_type\":\"text\",\"class\":\"Wdate\",\"disabled\":false},{\"display\":\"总加工费\",\"fields_name\":\"t_jiagongfei\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"总出口值\",\"fields_name\":\"t_chukou_zhi\",\"input_type\":\"text\",\"disabled\":false},{\"display\":\"总原料值\",\"fields_name\":\"t_yuanliao\",\"input_type\":\"text\",\"disabled\":false}]}

为什么后台 都加上了"\"
如果没有"\" 是可以还原对象的
怎样去掉"\"

php配置中开启了magic quotes gpc,所有get、post和cookie都会自动addslashes,输出时需要stripslashes解掉
但是你别在存数据库之前解,这就是为了防注入的

入库前如果配置文件,开启自动转义开关,那么就会在内容里加上“\”进行转义,入库之后就会看到有“\”。
所以读取数据的时候需要反转移stripslashes

加base64_encode,服?器再base64_decode可解???。
不?magic quotes gpc建??,新版本的php默?是?的。

你这是自找麻烦!
只需 field_json = obj1;
php $_POST 自然就保有全部提交数据

你这是自找麻烦!
只需 field_json = obj1;
php $_POST 自然就保有全部提交数据


服务器段如何提取  

我用echo $field_obj;  返回的是null

print_r($_POST);
什么都看到了

果然有效!多谢指点 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template