javascript - ajax跨域获取session的问题
最近在研究angularjs,想搞个前后端分离,结果发现ajax跨域请求时session丢失了?
前端www.book.com
后端api.book.com
后端设置了
<code>ini_set('session.cookie_domain', ".book.com"); header("Access-Control-Allow-Origin:*");</code>
所以现在单纯的前端发送ajax请求到后端返回数据是可以获得到的,
但前端发送请求到后端,后端打印session为null。
<code><pre class="brush:php;toolbar:false">array(0) { }
我做了实验,前端放了个php,test.php
<code><?php ini_set('session.cookie_domain', ".book.com"); session_start(); var_dump($_SESSION);</code></code>
使用www.book.com/test.php访问,结果是有的
<code>array(2) { ["d2d977c58444271d9c780187e93f80e5login"]=> array(2) { ["verify_code"]=> string(32) "748247cb9c625028d55e6a6bb90f7dc8" ["verify_time"]=> int(1464936279) } ["d2d977c58444271d9c780187e93f80e5resigiter"]=> array(2) { ["verify_code"]=> string(32) "2cfddfe2efaa2a0d78cd20c367df52f7" ["verify_time"]=> int(1464930066) } }</code>
也就是说我在跨二级域名直接访问,是成功能获取到session的,但在www.book.com的页面中ajax到api.book.com就拿不到了?
回复内容:
最近在研究angularjs,想搞个前后端分离,结果发现ajax跨域请求时session丢失了?
前端www.book.com
后端api.book.com
后端设置了
<code>ini_set('session.cookie_domain', ".book.com"); header("Access-Control-Allow-Origin:*");</code>
所以现在单纯的前端发送ajax请求到后端返回数据是可以获得到的,
但前端发送请求到后端,后端打印session为null。
<code><pre class="brush:php;toolbar:false">array(0) { }
我做了实验,前端放了个php,test.php
<code><?php ini_set('session.cookie_domain', ".book.com"); session_start(); var_dump($_SESSION);</code></code>
使用www.book.com/test.php访问,结果是有的
<code>array(2) { ["d2d977c58444271d9c780187e93f80e5login"]=> array(2) { ["verify_code"]=> string(32) "748247cb9c625028d55e6a6bb90f7dc8" ["verify_time"]=> int(1464936279) } ["d2d977c58444271d9c780187e93f80e5resigiter"]=> array(2) { ["verify_code"]=> string(32) "2cfddfe2efaa2a0d78cd20c367df52f7" ["verify_time"]=> int(1464930066) } }</code>
也就是说我在跨二级域名直接访问,是成功能获取到session的,但在www.book.com的页面中ajax到api.book.com就拿不到了?
需要在CORS的头里允许cookie,并且在发起ajax的时候设置withCredentials:true

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
