ajax and cookies

  • Thread starter Mr. SweatyFinger
  • Start date
M

Mr. SweatyFinger

OK I don't understand ajax, but my page makes a quick ajax call to another
page and returns the results.
that works ok.

That other page is supposed to set a cookie.

That doesn't seem to work.

Is that because ajax doesn't send all the headers and cookies, or am i being
a big dumb jerk?
 
N

Nathan Sokalski

When AJAX makes a call and recieves a response, the response is not the
sending of a Response object, it is a call to a JavaScript function on the
page using AJAX. Cookies are set when the Response object is set because
what causes a cookie to be set are headers in the HTTP. Because the Response
object, which is what the other page is setting the headers in, does not get
sent, you are never actually telling the browser to set the cookies. I will
admit that I am not yet an expert in AJAX, but I think that this is if not
the exact reason for your problem, it has something to do with the reason
and should help you find it. Good Luck!
 
L

Laurent Bugnion [MVP]

Nathan,

Nathan said:
When AJAX makes a call and recieves a response, the response is not the
sending of a Response object, it is a call to a JavaScript function on the
page using AJAX. Cookies are set when the Response object is set because
what causes a cookie to be set are headers in the HTTP. Because the Response
object, which is what the other page is setting the headers in, does not get
sent, you are never actually telling the browser to set the cookies. I will
admit that I am not yet an expert in AJAX, but I think that this is if not
the exact reason for your problem, it has something to do with the reason
and should help you find it. Good Luck!

This is not correct. When you send a XmlHttpRequest using the Web
browser, the request sent is a perfectly normal request. The response to
that is a perfectly normal response. Granted, it will often contain XML
and not HTML, but HTTP responses can do that.

Cookies should be handled correctly in AJAX too.

To Mr. Sweaty Finger: You must make sure that the page setting the
cookies does this correctly. To do that, use Fiddler to watch the HTTP
traffic between your IE and the web server. This will help you find out
if the cookies are not set correctly, or if they are set correctly but
incorrectly received.

HTH,
Laurent
 
G

George Ter-Saakov

Browser and Ajax call share cookie container. So if you are setting cookies
in a Ajax return it should work.
The only reason I think it might now work if you are making Ajax call to
another server.

Meaning that your page was served from www.mysite.com and you are making
Ajax call to www.anothersite.com
PS: www.mysite.com and site.com will be 2 different sites. URL must match
exactly (not case sensitive though).


George.
 
M

Mr. SweatyFinger

The answer is that the cookies expired immediately because i did not set an
expiration date

Since they expired they appeared to not have been set.

So yes, they did get transferred via the ajax

oops

thanks everyone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top