• TP5开发API接口-1.创建token、解决跨域、设置api接口返回格式


    1、解决TP5接口跨域问题

    说明:调用其他方法,会先执行TP5.0析构方法,然后再执行其他方法

    //解决跨域--析构方法
        protected function _initialize()
        {
            header('content-type:text/html;charset=utf-8');
            header('Access-Control-Allow-Origin: *');
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            header('Access-Control-Allow-Methods: GET, POST, PUT');
            ksort($_POST);
            ksort($_GET);
        }
    
     
    a3e0fe5fa47ba1ca5046c74d1857ce5e.png

    2、创建token方法

    //生成token操作
        public function create_token(){
            $token = md5(mt_rand(111111,999999).time());
            return $token;
        }
    
     
    f669df7a90626d7a5e423904c1c04cc7.png

    3.定义api接口返回数据格式,在common.php公共文件中配置

    //API数据返回接口,统一规定,-1:未登录,0:普通错误,1:返回成功
    function ajaxmsg($msg = "", $status = 1, $data = '', $errcode = '')
    {
        $json['msg'] = $msg;
        $json['status'] = $status;
        $json['data'] = $data;
        if ($errcode) {
            $json['errcode'] = $errcode;
        }
        echo json_encode($json, true);
        exit;
    }




  • 相关阅读:
    DataFrame转矩阵Np-Array
    十月15
    十月14
    十月14
    十月12
    十月10
    2016-02-22 有无网络 2
    2016-02-22 有无网络的提示 1 h m
    2016-02-20 web view
    20160220 下拉刷新 上拉加载
  • 原文地址:https://www.cnblogs.com/samRoot/p/12960864.html
Copyright © 2020-2023  润新知