Recently I am studying angularjs and want to implement frontend and backend separation. The result shows that the session is lost in ajax cross-origin requests? {Code...} is set at the backend of the frontend www.book.com backend api.book.com ...} therefore, it is possible to simply send an ajax request to the front end to return data to the back end, but the front end sends... recently I am studying angularjs and want to implement frontend and backend separation. The result shows that the session is lost in ajax cross-origin requests?
Frontend www.book.com
Backend api.book.com
Backend settings
ini_set('session.cookie_domain', ".book.com");header("Access-Control-Allow-Origin:*");
Therefore, it is now possible to send an ajax request to the backend to return data,
However, when the frontend sends a request to the backend, the backend printing session is null.
array(0) {}
I did an experiment and put a php, test. php on the front end.
Use www.book.com/test.php to check whether the result is correct.
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) } }
That is to say, when I access a session directly from a cross-second-level domain name, I can get the session successfully, but I cannot get the session from ajax to api.book.com on the webpage www.book.com?
Reply content:
Recently I am studying angularjs and want to implement frontend and backend separation. The result shows that the session is lost in ajax cross-origin requests?
Frontend www.book.com
Backend api.book.com
Backend settings
ini_set('session.cookie_domain', ".book.com");header("Access-Control-Allow-Origin:*");
Therefore, it is now possible to send an ajax request to the backend to return data,
However, when the frontend sends a request to the backend, the backend printing session is null.
array(0) {}
I did an experiment and put a php, test. php on the front end.
Use www.book.com/test.php to check whether the result is correct.
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) } }
That is to say, when I access a session directly from a cross-second-level domain name, I can get the session successfully, but I cannot get the session from ajax to api.book.com on the webpage www.book.com?
You must allow cookies in the CORS header and set withCredentials: true when initiating ajax.