ThinkPHP3.2.3 解决跨域问题
2022-07-28 14:14:58
292
{{single.collect_count}}

使用前后端分离模式开发时,前端调用接口Chrome浏览器报错:
在这里插入图片描述
解决方案:
在Controller模块下新建BaseController.class.php

<?phpnamespace Home\Controller;use Think\Controller;class BaseController extends Controller{public function _initialize(){header('Content-Type:application/json; charset=utf-8');header("Access-Control-Allow-Origin: *");header('Access-Control-Allow-Methods:OPTIONS,POST,PUT,DELETE');}}

之后其他控制器继承这个类

class IndexController extends BaseController

此时调用能够正常获得接口数据
在这里插入图片描述

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}