$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
if (is_callable([$response, 'header'])) {
$response->header('Access-Control-Allow-Origin', $request->server('HTTP_ORIGIN'));
$response->header('Access-Control-Allow-Credentials', 'true');
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept, Authorization, X-XSRF-TOKEN, X-URL-PATH, x-access-token, withcredentials');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS, DELETE');
}
$response->send();
$kernel->terminate($request, $response);
import Axios from 'axios'
import baseURL from '_conf/url'
import { Message } from 'iview'
import Cookies from 'js-cookie'
import { TOKEN_KEY } from '@/libs/util'
Axios.defaults.withCredentials = true
class httpRequest {
......
}
同样的后端代码,之前iview-admin1.x版本可以正常设置Set-Cookie头信息,用了iview-admin2.0之后,在请求接口之后请求头信息里面没有 Set-Cookie,求大手子解答。。。已困扰一下午,求解脱。
请注意你的浏览器版本,如果为chrome较高的版本,引入了更加严格的安全机制,withCredentials=true即时这样跨域cookie也无法携带。我遇到了同样的问题,无奈放弃cookie,改用header传值,前端数据存storage里面。如果为较新版本的chrome就会有这个问题,如果为搜狗、360之类的浏览器的极速模式,一般考虑兼容性问题内核版本不会太高,是不会出现这个问题的
谢谢解答。之前是各种百度都解决不了问题,就放弃跨域cookie了。后来是用了header保存信息,后端代码
// app/Http/Middleware/ResponseWrapper.php 里面 handle 方法
// 通过x-token来获取sessionid
if ($request->hasHeader('x-token')) {
\Session::setId($request->header('x-token'));
\Session::start();
}
有人解决这个问题了吗?
求助啊
mockjs的锅,这货拦截了请求并修改了withCredentials
Most helpful comment
请注意你的浏览器版本,如果为chrome较高的版本,引入了更加严格的安全机制,withCredentials=true即时这样跨域cookie也无法携带。我遇到了同样的问题,无奈放弃cookie,改用header传值,前端数据存storage里面。如果为较新版本的chrome就会有这个问题,如果为搜狗、360之类的浏览器的极速模式,一般考虑兼容性问题内核版本不会太高,是不会出现这个问题的