Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

jquery ajax call not sending cookie

I have a jQuery ajax call across a subdomain that works correctly except it's not sending the cookie. Is there any way to accomplish this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This sounds like expected behavior to me. Cookies are per domain (and that includes subdomains). But I think you can force it with something like this:

$.ajax({
   headers: {'Cookie' : document.cookie },
   url: "sub.domain.com",
   success: function(){ ...

This is totally untested so let me know if it works ;)

EDIT: There is an alternative solution available using:

xhrFields: {
    withCredentials: true
}

Check here: How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?.

Also, you can set the cookies in PHP so that they are valid across all your subdomains. Something like this:

ini_set('session.cookie_domain', '.example.com')

Note the '.' before the domain - that will set the cookie for example.com and all its subdomains.

You can set session.cookie_domain in your app using the above or set it in your php.ini.

The above is stolen from here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...