静态页面ajax请求操作和json格式解析处理

作者:Jasmine - 2016年06月30日

html页面中发送一个ajax请求到服务端,实现静态无刷新提示并做数据处理操作! 

此处json需要格式话一下,格式化之后

$.ajax({ 
   type:"POST",
    url :"{#:U('Account/changeActiviteStatus')#}",
   data:{ uid:id },
   success:function(data,status){ 
       json = JSON.parse(data) //格式化json 格式话之后,alert弹出的是 (object.object),
       然后其他的用法,就和php的差不多了 alert(json.msg);
        return false; 
        if(data.msg){ swal("error",data.msg); return false; } 
        swal("success","操作成功!"); //注释: swal是一个弹出插件 
        window.location.href="{#:U('AccountCodes/index','','')#}"; },
        error:function(){ swal("error","发送请求失败,请重新操作!"); } });

php服务端处理(服务端直接把提示信息专程json数组,返回给页面即可,类似于这种:{“msg”:”\u66f4\u6539\u7528\u6237\u72b6\u6001\u5931\u8d25\uff01″})

public function changeActiviteStatus(){ 
   $uid = (string)I('uid',0); 
   $account = new AccountService(); 
   $codes = new RegisterCodeService(); 
   $res = $account->changeActiviteStatus($uid); 
   $bindCode= $codes->bindUidCode($uid); 
   if($res && $bindCode){ return true; }
    elseif(!$res){ print_r(json_encode(array('msg'=>'更改用户状态失败!'))); }
    elseif(!$bindCode){ print_r(json_encode(array('msg'=>'绑定申请码失败!'))); }
    else{ print_r(json_encode(array('msg'=>'未知数据库错误!'))); } }


本文作者: Jasmine

本文链接: https://www.jianbaizhan.com/article/459.html

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!


 请勿发布不友善或者负能量的内容。审查将对发布广告等违规信息进行处罚!