项目场景:
layui+php
用layui获取 table.cache[“你的lay-id”]; 时,得到的对象是object,如果直接放到 ajax的data post不了对象。后端接收也有错误。
问题描述:
layui的table.cache[lay-id],直接通过$.ajax的data:table.cache[lay-id],是无法上传正确的参数的。
$.ajax({
....
data:table.cache["你的lay-id"],
....
})
原因分析:
table.cache[“你的lay-id”]是object类型导致post并不完整
解决方案:
第一步:
var TbRows = table.cache["你的lay-id"];
var str_TbRows = JSON.stringify(TbRows);
$.ajax({
....
data:{info:str_TbRows},
....
});
第三步:
后端php必须是:
$info=json_decode(html_entity_decode(urldecode($_POST["info"])), 1);
总结:
解决方法:
(1)obj需要转换成字符串 JSON.stringify(table.cache[lay-id])
(2)用ajax的data:{info:JSON.stringify(table.cache[lay-id])}
(3)后端接收需要转换成数组变量 json_decode(html_entity_decode(urldecode($_POST[“info”])), 1);