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

WBOY
Release: 2016-10-11 14:23:10
Original
1182 people have browsed it

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 in cd.xxx.com to request www.xxx.com will generate a new one session_id causes verification failure!
Is there any way to deal with it? Thanks in advance!

Reply content:

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 in cd.xxx.com to request www.xxx.com will generate a new one session_id causes verification failure!
Is there any way to deal with it? Thanks in advance!

This problem belongs to the problem of Ajax carrying cookies across domains. I found a solution in a blog post.

Native ajax request method:

<code>var xhr = new XMLHttpRequest();  
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  
xhr.withCredentials = true; //支持跨域发送cookies
xhr.send();</code>
Copy after login

jquery post method request:

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

Server side settings:

<code>header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");</code>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!