您现在的位置是: 首页 > 前端开发 ThinkPHP 解决跨域访问问题
ThinkPHP 解决跨域访问问题
2020-05-20 【前端开发】 3292人已围观 6119次浏览
简介ThinkPHP 解决跨域访问问题
单页面应用访问TP写的接口时出现跨域问题,解决方案如下
namespace app\api\behavior;
class CORS
{
public function appInit(&$params)
{
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: access_token, Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
if (request()->isOptions()) {
exit();
}
}
}
在application\api\behavior下加入CORS.php文件,内容就是上面的内容
然后在tags.php中加入
// 应用初始化
'app_init' => [
'app\\api\\behavior\\CORS'
],
再次使用单页应用访问接口,则不会出现跨域提示
很赞哦! (0)
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- Debian apt 使用国内镜像
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- nuxt 项目完整部署方案
站长推荐
猜你喜欢
- sentinel directive while not in sentinel mode 问题解决
- Docker 批量操作命令
- Nestedset 出现 Node must exists. 错误解决方案
- nginx 出现 the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in 错误解决方案
- SqlServer 优化技巧
- CentOS 7 安装 Golang 环境
- Redis 提示 MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk 解决方案
- Win10 安装适用于Linux的Windows子系统
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- 防止Time Machine备份Parallels Desktop的虚拟硬盘文件