当前位置: 代码迷 >> 综合 >> 07thinkphp 第8天 allowCrossDomain 跨域知识点!
  详细解决方案

07thinkphp 第8天 allowCrossDomain 跨域知识点!

热度:45   发布时间:2023-10-10 23:35:43.0

开启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();