您现在的位置是: 首页 > 前端开发 ThinkPHP 解决跨域访问问题
ThinkPHP 解决跨域访问问题
2020-05-20 【前端开发】 3805人已围观 6760次浏览
简介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 项目完整部署方案
站长推荐
猜你喜欢
- 查看MySQL默认读取的配置文件
- LNMP 手动更新 SSL 证书
- SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
- SpringBoot @NotBlank 不生效问题
- Zookeeper 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
- SqlServer 查询包含指定字段的存储过程
- 迁移 Docker 目录
- docker mysql 导入 SQL 脚本
- mybatis中大于等于小于等于的写法
- Win10 安装适用于Linux的Windows子系统