最近瞎折腾,弄了下uniapp写了个简单的h5页面算是新增了网站的页面吧。但是遇到了跨域的问题,之前再tp5中的解决办法已经不起作用了。
eader("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods", "POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
现在tp6自带了中间件跨域但是不知道为什么没用
没办法只有自己写一个简单的中间件来用吧
目录结构如下:
代码如下:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/12/23
* Time: 15:10
* manongw.com 承接小程序,网页,app,h5等开发
*/
namespace app\middleware;
class AllowCrossDomain{
/**
* 设置跨域
* @param $request
* @param \Closure $next
* @return mixed|void
*/
public function handle($request, \Closure $next)
{
$response = $next($request);
$origin = $request->header('Origin', '');
//OPTIONS请求返回204请求
if ($request->method(true) === 'OPTIONS') {
$response->code(204);
}
$response->header([
'Access-Control-Allow-Origin' => $origin,
'Access-Control-Allow-Methods' => 'GET,POST,PUT',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => '*',
]);
return $response;
}
}
之后在app中添加如下代码即可
\app\middleware\AllowCrossDomain::class