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設置不成功 請問是什么原因?
發(fā)起ajax請求的域名是domain.com,被請求的域名是api.domain.com
瀏覽器有限制,AI助手問下 “ajax請求時cookie設置不成功“ 就有答案了。
作為程序員要與時俱進,學會用AI。我現(xiàn)在都是讓AI寫代碼+測試用例,然后粘貼過去運行下,真的很省時間。
真的,程序員學會用AI吧,尤其這種基礎問題。