如果js 中将提交的数据进行了序列化(如通过JSON.stringify()),通过Ajax提交后,有时,浏览器自动转义json数据内部的双引号,加了反斜杠,这里需要接收后PHP过滤去掉,否则会影响PHP识别json格式,致使json_decode不能进行反序列化。
PHP对json格式要求比较严格,最好按照json规定标准化。
下面列出我的控制器controller文件对json的处理代码:
public function get_pos(){
$a_id = $_POST['id'] ? $_POST['id'] : '';
$os = $_POST['os'] ? $_POST['os'] : 'android';
if($a_id && $os){
$data = array();
$data['res'] = 0;
if($os == 'android'){
$data['data'] = array(
0 => array(
'title' => '玩啥首页',
'status' => 0,
'id' => 115610000
),
1 => array(
'title' => '玩啥广告位',
'status' => 0,
'id' => 115610001
),
2 => array(
'title' => '游戏中心专题列表',
'status' => 0,
'id' => 114510002
),
3 => array(
'title' => '游戏中心文章列表',
'status' => 0,
'id' => 114510003
)
);
}elseif($os == 'ios'){
$data['data'] = array(
0 => array(
'title' => '玩啥首页',
'status' => 0,
'id' => 115710000
),
1 => array(
'title' => '玩啥广告位',
'status' => 0,
'id' => 115710001
),
2 => array(
'title' => '游戏中心专题列表',
'status' => 0,
'id' => 113310002
),
3 => array(
'title' => '游戏中心文章列表',
'status' => 0,
'id' => 113310003
)
);
}
if($os == 'android'){
if(mod_wansha :: selecthomepage($a_id)){
$data['data'][0]['status'] = 1;
}
if(mod_wansha :: selectadv($a_id)){
$data['data'][1]['status'] = 1;
}
if(mod_wansha :: select_imgp($a_id, $pid = substr($data['data'][2]['id'],0,-5))){
$data['data'][2]['status'] = 1;
}
if(mod_wansha :: select_ap($a_id, $pid = substr($data['data'][3]['id'],0,-5))){
$data['data'][3]['status'] = 1;
}
$data['res'] = 1;
}elseif($os == 'ios'){
if(mod_wansha :: selecthomepage1157($a_id)){
$data['data'][0]['status'] = 1;
}
if(mod_wansha :: selectadv1157($a_id)){
$data['data'][1]['status'] = 1;
}
if(mod_wansha :: select_imgp($a_id, $pid = substr($data['data'][2]['id'],0,-5))){
$data['data'][2]['status'] = 1;
}
if(mod_wansha :: select_ap($a_id, $pid = substr($data['data'][3]['id'],0,-5))){
$data['data'][3]['status'] = 1;
}
$data['res'] = 1;
}
}
//echo $_GET['callback'] . '(' . json_encode($data) . ')';
echo json_encode($data);
}
public function update_pos(){
$a_id = $_POST['id'] ? $_POST['id'] : '';
$os = $_POST['os'] ? $_POST['os'] : 'android';
$a_type = $_GET['a_type'] ? $_GET['a_type'] : 0;
$newdata['a_id'] = $a_id;
$newdata['adv_type'] = $a_type;
$newdata['adv_a_id'] = $a_id;
$newdata['adv_isexists'] = 0;
$newdata['adv_sort'] = 0;
$newdata['ap_sort'] = 0;
$newdata['ip_sort'] = 0;
if($a_id){
$data = array();
$flag = array();
$data['data'] = array();
if($_POST['status']){
$str = $_POST['status'];
//为标准化,可给int型/布尔型正则匹配加上双引号
//$tmpstr = preg_replace('/:(\w+)/is', ':"$1"', $str);
//浏览器自动转义内部的双引号,加了反斜杠,这里需要PHP过滤去掉,否则会影响PHP识别json格式,json_decode不能进行反序列化。
$tmpstr = preg_replace('/\\\/', '', $str);
$tmpstr = trim($tmpstr); //去除字符串首尾处的空白字符(或者其他字符)
$data['data'] = json_decode($tmpstr);
//debug($data['data']);
}
for($i = 0, $len = count($data['data']); $i < $len; $i++){
$optionid = substr($data["data"][$i]->posid,-5,5);
$productid = substr($data['data'][$i]->posid,0,-5);
$statuscode = $data['data'][$i]->posstatus;
switch($optionid){
case 10000:
if($productid == 1156 && $statuscode == 1 && $os == 'android' && !mod_wansha :: selecthomepage($a_id) ){
$ret = mod_wansha :: insert_homepage($newdata);
$flag[$i] = 1;
}elseif($productid == 1157 && $statuscode == 1 && $os == 'ios' && !mod_wansha :: selecthomepage1157($a_id)){
$ret = mod_wansha :: insert_homepage1157($newdata);
$flag[$i] = 1;
}
break;
case 10001:
if($productid == 1156 && $statuscode == 1 && $os == 'android' && !mod_wansha :: selectadv($a_id) ){
$ret = mod_wansha :: insert_adv($newdata);
$flag[$i] = 1;
}elseif($productid == 1157 && $statuscode == 1 && $os == 'ios' && !mod_wansha :: selectadv1157($a_id)){
$ret = mod_wansha :: insert_adv1157($newdata);
$flag[$i] = 1;
}
break;
case 10002:
if(!mod_wansha :: select_imgp($a_id,$productid) && $statuscode == 1){
mod_wansha :: insert_imgp($newdata,$productid);
$flag[$i] = 1;
}
case 10003:
if(!mod_wansha :: select_ap($a_id,$productid) && $statuscode == 1){
mod_wansha :: insert_ap($newdata,$productid);
$flag[$i] = 1;
}
}
}
if(in_array(1,$flag)){
$data['res'] = 1;
}
}
/*跨域调用的时候,服务端会接收到一段$_GET['callback']的字符串,你需要在服务端将这段字符串拼接在需要返回的数据的前面共同返回*/
echo $_GET['callback'] . '(' . json_encode($data) . ')';
}