一個簡單的方法,就是在json_encode($data) ,后面添加一個參數 json_encode($data, JSON_UNESCAPED_UNICODE);
寫一個簡單的代碼
public function appLog($id,$age)
{
$User = M('User');
$where['id'] = $id;
$where['age'] = $age;
$data = $User->where($where)->select();
if ($data){
return $this->api_return('請求成功!',0,$data);
}else{
return $this->api_return('請求失敗!',-1,'null');
}
}
public function api_return($msg = '', $code = -1, $data = null)
{
$output = array(
'message' => $msg,
'code' => $code,//-1表示錯誤 0 表示正確返回數據
'data' => $data
);
// 返回JSON數據格式到客戶端
header('Content-Type:application/json; charset=utf-8');
$output = json_encode($output, JSON_UNESCAPED_UNICODE);
echo $output;
}
{"message":"請求成功!","code":0,"data":[{"id":"2","name":"sohu","age":"19","email":"zzy@souhu.com","status":"1","sex":"1"}]}