Cross-origin session acquisition using javascript-ajax

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.