After session sharing is allowed, the ajax request generates a new session_id

Source: Internet
Author: User
The current situation is: the session_id generated by directly accessing cd.xxx.com is the same as the session_id generated by accessing www.xxx.com, but using ajax to request www.xxx.com in cd.xxx.com will generate a new session_id, leading to verification failure! Is there a solution ?, Thank you! The current situation is:
The session_id generated by directly accessing cd.xxx.com is consistent with the session_id generated by accessing www.xxx.com.
However, when you use ajax to request www.xxx.com in cd.xxx.com, a new session_id will be generated, leading to verification failure!
Is there a solution ?, Thank you!

Reply content:

The current situation is:
The session_id generated by directly accessing cd.xxx.com is consistent with the session_id generated by accessing www.xxx.com.
However, when you use ajax to request www.xxx.com in cd.xxx.com, a new session_id will be generated, leading to verification failure!
Is there a solution ?, Thank you!

This problem is caused by cross-origin Cookie of Ajax. I found a solution for a blog post.

Native ajax request method:

Var xhr = new XMLHttpRequest (); xhr. open ("POST", "http://xxxx.com/demo/ B /index.php", true); xhr. withCredentials = true; // supports cross-origin sending cookiesxhr. send ();

Jquery post method request:

 $.ajax({    type: "POST",    url: "http://xxx.com/api/test",    dataType: 'jsonp',    xhrFields: {withCredentials: true},    crossDomain: true,})

Server side settings:

header("Access-Control-Allow-Credentials: true");header("Access-Control-Allow-Origin: http://www.xxx.com");

Thanks to the original author:
Http://blog.sina.com.cn/s/blo...

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.