您现在的位置是: 首页 > 前端开发 ThinkPHP 解决跨域访问问题
ThinkPHP 解决跨域访问问题
2020-05-20 【前端开发】 2427人已围观 4242次浏览
简介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
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- Debian apt 使用国内镜像
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- nuxt 项目完整部署方案
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
站长推荐
猜你喜欢
- Linux 查看磁盘、磁盘分区、挂载磁盘、卸载磁盘操作
- Vue 监听回车 el-input 和 input 使用方法不同
- sentinel directive while not in sentinel mode 问题解决
- Vue 路由跳转后不在顶部
- Linux 服务器之间传输大文件(压缩文件、查看MD5、后台传输)
- CentOS 安装Node.js
- 获取阿里云CDN真实IP
- 自建Ngrok服务端
- SqlServer 查询包含指定字段的存储过程
- SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS