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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章