1. 程式人生 > >tp5.0定義路由 +隱藏介面url地址

tp5.0定義路由 +隱藏介面url地址

php

public function api(){
      return view();
    }
    public function manage($id){
       $test = input('id');
       if($test==1){
         $data = [
           'code' => '200',
           'title' => 'test',
           'api_mag' => '測試成功'
         ];
         echo json_encode($data);
       }else{
         echo json_encode('400');
       }
    }

Route

Route::rule('new','index/index/api');
Route::rule('api_index/:id','index/index/manage');

html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.get("/api_index/1.php",function(data){
        var obj = JSON.parse(data)
        $("#manage").append(obj.api_mag)
        });
    });
});
</script>
</head>
<body>

<button>傳送一個 HTTP GET 請求並獲取返回結果</button>
<div id="manage"></div>
</body>
</html>