您的位置:首页 > 编程语言 > PHP开发

PHP 封装ajax返回数据格式

2017-06-26 16:21 302 查看
protected function ajaxReturn($data, $type='') {
if(empty($type)) $type  =   'JSON';
switch (strtoupper($type)){
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
return json_encode($data);
case 'XML'  :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
return xml_encode($data);
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$handler  =   isset($_GET['callback']) ? $_GET['callback'] : 'chatcallback';
return $handler.'('.json_encode($data).');';
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
return $data;
default     :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
return json_encode($data);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php