JSON传数组对象到后台的过程和接收(TP框架)

版权声明:本文为博主原创文章,转载文章请联系博主 [email protected] https://blog.csdn.net/qq_35070711/article/details/78622709

TP框架中,前台发送数组类型的json字符串到后台,后台I方法获取到的json字符串会改变(可以在chrome下抓包看response),
用$_POST获取之后再用json_decode转化为数组就能成功;
在I方法获取过程中使用strip_tags替代掉htmlspecialchars:json_decode(I(‘data’,”,’strip_tags’))
再打印出来就成功了。即使在I方法第三个参数使用null也无法置换掉默认的过滤方法,所以要用其他过滤手段替换

前台发送数组对象

{
    "data":[
        {
            "pid":"22",
            "quantity":"22"
        },
        {
            "pid":"3",
            "quantity":"66"
        },
    ]

}

后台接收data数组对象
方法1 :

$data = I('post.data', '', 'strip_tags');

方法2 :

$data = json_decode(I('data','','strip_tags'));

然后再依次读取data中的字段

注意:json_decode,第二个参数为true,返回数组,为false,返回对象

猜你喜欢

转载自blog.csdn.net/qq_35070711/article/details/78622709
今日推荐