在本地
跨域
使用第一种:
<?php
/**
* Created by PhpStorm.
* User: M_jh
* Date: 2019/1/18
* Time: 15:04
*/
namespace app\api\behavior;
use think\Response;
class CORS
{
public function appInit(&$params)
{
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: token, Origin, X-Requested-With, Content-Type, Accept, Authorization");
header('Access-Control-Allow-Methods: POST,GET,PUT,DELETE');
if (request()->isOptions()){
exit();
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'app_init' => [
'app\\api\\behavior\\CORS'
],
使用方法二:
<?php
/**
* Created by PhpStorm.
* User: M_jh
* Date: 2019/1/18
* Time: 21:16
*/
namespace service;
class ToolsService
{
public static function corsOptionsHandler()
{
if (request()->isOptions())
{
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
header('Access-Control-Max-Age:1728000');
header('Content-Type:text/plain charset=UTF-8');
header('Content-Length: 0', true);
header('status: 204');
header('HTTP/1.0 204 No Content');
}else{
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
/**
* Created by PhpStorm.
* User: M_jh
* Date: 2019/1/18
* Time: 21:19
*/
namespace app\api\controller\v1;
use think\Controller;
use ervice\ToolsService;
class BaseController extends Controller
{
/**
* 基础接口
* @param Request|null $request
*/
public function __construct(Request $request = null)
{
// CORS 跨域 Options 检测响应
ToolsService::corsOptionsHandler();
}
}
问题出在跨域,但是借助网上这两种方法都不成功,都是同样的返回提示。现在问题不是这两种方式,搞不清楚问题出在哪儿了?