虽然这个功能自己几乎不用 ,看到网友经常要处理这个问题,顺手发一个。
1、在要输出json头部代码
2.改造printerror和printerror2,增加一个参数$statuscode,不写的状态200
- -------------------------------
- define('API_WORK', true);
- -------------------------------
- //错误提示
- function printerror($error="",$gotourl="",$ecms=0,$noautourl=0,$novar=0, $statuscode = 200){
- global $empire,$editor,$public_r,$ecms_config;
- if($editor==1){$a="../";}
- elseif($editor==2){$a="../../";}
- elseif($editor==3){$a="../../../";}
- else{$a="";}
- if($ecms==1||$ecms==9)
- {
- $a=ECMS_PATH.'e/data/';
- }
- if(strstr($gotourl,"(")||empty($gotourl))
- {
- if(strstr($gotourl,"(-2"))
- {
- $gotourl_js="history.go(-2)";
- $gotourl="javascript:history.go(-2)";
- }
- else
- {
- $gotourl_js="history.go(-1)";
- $gotourl="javascript:history.go(-1)";
- }
- }
- else
- {$gotourl_js="self.location.href='$gotourl';";}
- if(empty($error))
- {$error="DbError";}
- if($ecms==9)//前台弹出对话框
- {
- @include $a.LoadLang("pub/q_message.php");
- $error=empty($novar)?$qmessage_r[$error]:$error;
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- echo"<script>alert('".$error."');".$gotourl_js."</script>";
- db_close();
- $empire=null;
- exit();
- }
- elseif($ecms==8)//后台弹出对话框
- {
- @include $a.LoadLang("pub/message.php");
- $error=empty($novar)?$message_r[$error]:$error;
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- echo"<script>alert('".$error."');".$gotourl_js."</script>";
- db_close();
- $empire=null;
- exit();
- }
- elseif($ecms==7)//前台弹出对话框并关闭窗口
- {
- @include $a.LoadLang("pub/q_message.php");
- $error=empty($novar)?$qmessage_r[$error]:$error;
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- echo"<script>alert('".$error."');window.close();</script>";
- db_close();
- $empire=null;
- exit();
- }
- elseif($ecms==6)//后台弹出对话框并关闭窗口
- {
- @include $a.LoadLang("pub/message.php");
- $error=empty($novar)?$message_r[$error]:$error;
- echo"<script>alert('".$error."');window.close();</script>";
- db_close();
- $empire=null;
- exit();
- }
- elseif($ecms==0)
- {
- @include $a.LoadLang("pub/message.php");
- $error=empty($novar)?$message_r[$error]:$error;
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- @include($a."message.php");
- }
- else
- {
- @include $a.LoadLang("pub/q_message.php");
- $error=empty($novar)?$qmessage_r[$error]:$error;
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- @include($a."../message/index.php");
- }
- db_close();
- $empire=null;
- exit();
- }
3、使用自动加载功能和异常处理。以上3个步骤完成,一起来体验吧!
- //错误提示2:直接文字
- function printerror2($error='',$gotourl='',$ecms=0,$noautourl=0, $statuscode = 200){
- global $empire,$public_r;
- if(strstr($gotourl,"(")||empty($gotourl))
- {
- if(strstr($gotourl,"(-2"))
- {
- $gotourl_js="history.go(-2)";
- $gotourl="javascript:history.go(-2)";
- }
- else
- {
- $gotourl_js="history.go(-1)";
- $gotourl="javascript:history.go(-1)";
- }
- }
- else
- {$gotourl_js="self.location.href='$gotourl';";}
- if($ecms==9)//弹出对话框
- {
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- echo"<script>alert('".$error."');".$gotourl_js."</script>";
- }
- elseif($ecms==7)//弹出对话框并关闭窗口
- {
- //修改的
- if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode);
- echo"<script>alert('".$error."');window.close();</script>";
- }
- else
- {
- @include(ECMS_PATH.'e/message/index.php');
- }
- db_close();
- exit();
- }
技巧:define('API_WORK', true);可以改为动态的,不是新写的文件时还有必要,传一下$_GET['json'] = 1就样就可以解决了
这个就可以原代码一个不动了
- define('API_WORK', isset($_GET['json']));