跨域问题解决分享
配置文件内加入前端域名
文件位置:config/buildadmin.php
return [
// 允许跨域访问的域名
'cors_request_domain' => 'localhost,127.0.0.1,www.xxx.com',
Header参数问题
文件位置:app/common/middleware/AllowCrossDomain.php
方法一:框架api应用内跨域中间件Access-Control-Allow-Headers
参数,已经设置了可以跨域的header
字段,如果有自定义的header
参数,需要加入自定义字段。
<?php
declare (strict_types=1);
namespace app\common\middleware;
use Closure;
use think\Request;
use think\Response;
use think\facade\Config;
/**
* 跨域请求支持
* 安全起见,只支持了配置中的域名
*/
class AllowCrossDomain
{
protected array $header = [
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => 1800,
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers' => 'token,platform,think-lang, server, ba_user_token, ba-user-token, ba_token, ba-token, batoken, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
];
...
方法二:创建新的应用,在新应用内做业务。
serve参数
文档地址:https://ba.buildadmin.com/senior/server/apiDebug.html
2个回答默认排序 投票数排序

ljt0722
这家伙很懒,什么也没写~
1月前
正解

fastknife
这家伙很懒,什么也没写~
1月前
还有一种报错的场景,开发环境下, 后端使用buildadmin, 前端使用uniapp。
无论cors_request_domain
设置什么也无效。
其实是接口的请求发到了index.html那去了。 所以前端接口域名要改成http://DOMAIN/index.php
请先登录
域名不用带协议的