您现在的位置是: 首页 > 前端开发 ThinkPHP 解决跨域访问问题
ThinkPHP 解决跨域访问问题
2020-05-20 【前端开发】 2710人已围观 5186次浏览
简介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
站长推荐
猜你喜欢
- Laravel Dingo/api 出现 The version given was unknown or has no registered routes.报错
- SpringBoot @NotBlank 不生效问题
- CentOS 安装Node.js
- Linux no space left on device 出现设备上没有空间问题
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- mac idea spring boot 启动慢
- LNMP定时备份任务教程(支持排除指定文件夹)
- PHP 中list()出现Undefined offset: 0错误
- 【代码片段】MySQL新建表添加基础字段
- Windows CMD 常用命令