Home  >  Article  >  Backend Development  >  前端串js对象数组给php 所用框架thinkphp

前端串js对象数组给php 所用框架thinkphp

WBOY
WBOYOriginal
2016-06-23 13:39:58944browse

//js 对象<script>    person[]=new Object();    person[0].firstname="Bill";    person[0].lastname="Gates";     person[1].firstname="Bill";    person[1].lastname="Gates";    //将对象转为json字符串    var jsonstr=JSON.stringify(person);    //传递给php    $.post('url',{obj:jsonstr},function(data){},'json');</script>//php 代码public function get_json(){    //获取json字符串    //注意 用thinkphp 框架 最好不要用框架自带的I方法获取数据,因为I方法在过滤字符串的同时会把字符串内部的双引号转义,导致解析json失败 返回null.此时若用json_last_error()函数获取 介绍返回提示,返回值可能是4 表示语法错误。    $str=$_POST['obj'];    //解析json    $str=json_decode($str);}

Statement:
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