public function index(Request $request)
{
$startTime = microtime(1);
$response = response();
$geo = [
'code' => 1,
'data' => [
'ip' => $request->header('x-real-ip'),
'ua' => $request->header('user-agent'),
'ref' => $request->header('referer'),
],
'cache' => 1,
'msg' => 'success'
];
$ccCookieName = md5($geo['data']['ip']);
$geo['data']['cc'] = $request->cookie($ccCookieName);
if ($geo['data']['cc'] === null)
{
$geo['cache'] = 0;
$geoIp = new Reader(config('app.database_path') . '/GeoLite2-Country.mmdb');
try {
$geo['data']['cc'] = $geoIp->country($geo['data']['ip'])->country->isoCode;
$response->cookie($ccCookieName, $geo['data']['cc'], 7 * 24 * 3600, '/', '.domain.com');
} catch (AddressNotFoundException|InvalidDatabaseException $e) {
$geo['code'] = 0;
$geo['msg'] = $e->getMessage();
}
}
$geo['time'] = microtime(1) - $startTime;
return $response
->header('Content-Type', 'application/json')
->withBody(json_encode($geo, JSON_UNESCAPED_UNICODE));
}
我直接通过浏览器访问这个页面时,cookie设置是成功的,但当我通过ajax请求这个页面时,cookie设置不成功 请问是什么原因?
发起ajax请求的域名是domain.com,被请求的域名是api.domain.com
浏览器有限制,AI助手问下 “ajax请求时cookie设置不成功“ 就有答案了。
作为程序员要与时俱进,学会用AI。我现在都是让AI写代码+测试用例,然后粘贴过去运行下,真的很省时间。
真的,程序员学会用AI吧,尤其这种基础问题。