开启Cors跨域
<?phpnamespace app\api\controller;use app\api\model\Cates;
use think\Controller;
//引入request
use think\Request;class Category extends Controller
{//编写lst方法public function lst(Request $request){#$pid = $request->param('pid');#调用模型获取数据 返回数据$cates = Cates::field('id,name,parent_id as parentId,is_parent as isParent,sort')->where('parent_id',$request->pid)->select();#设置响应头/*$headers = ['Access-Control-Allow-Origin'=>'*','Access-Control-Allow-Methods'=>'*','Access-Control-Allow-Credentials'=>'false','Access-Control-Allow-Headers'=>'content-type'];*/#返回json数据#return json($cates)->header($headers);return json($cates);}
}
Route::get('api/item/category/list','api/category/lst')->allowCrossDomain();