您现在的位置是: 首页 > 前端开发 ThinkPHP 解决跨域访问问题
ThinkPHP 解决跨域访问问题
2020-05-20 【前端开发】 2827人已围观 5510次浏览
简介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 自动填充无效问题解决
- nuxt 项目完整部署方案
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
站长推荐
猜你喜欢
- Nginx转发阿里云OSS内网流量
- PHP 在执行 composer install 时出现提示 PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>
- 记一次Java MessageFormat.format踩坑
- npm 出现 Unexpected token < in JSON at position 21330
- 查看MySQL默认读取的配置文件
- linux下各文件夹的结构说明及用途介绍
- 使用 OpenSSL 将 pfx 格式证书转为 pem 格式
- SqlServer 查询包含指定字段的存储过程
- 防止Time Machine备份Parallels Desktop的虚拟硬盘文件
- gulp3 在 node12 上运行出现异常 primordials is not defined