ajax and cookies

Discussion in 'ASP .Net' started by Mr. SweatyFinger, Feb 5, 2007.

  1. 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?
    Mr. SweatyFinger, Feb 5, 2007
    #1
    1. Advertising

  2. 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!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Mr. SweatyFinger" <> wrote in message
    news:%...
    > 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?
    >
    >
    >
    Nathan Sokalski, Feb 5, 2007
    #2
    1. Advertising

  3. Nathan,

    Nathan Sokalski wrote:
    > 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
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion [MVP], Feb 5, 2007
    #3
  4. 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.


    "Mr. SweatyFinger" <> wrote in message
    news:%...
    > 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?
    >
    >
    >
    George Ter-Saakov, Feb 5, 2007
    #4
  5. The answer

    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
    Mr. SweatyFinger, Feb 5, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,487
    Hans Kesting
    Feb 3, 2005
  2. Andy Fish
    Replies:
    3
    Views:
    6,526
    Fredrik Lindner
    Nov 6, 2003
  3. minnie
    Replies:
    1
    Views:
    686
    Andrew Thompson
    Dec 13, 2006
  4. archana
    Replies:
    1
    Views:
    506
  5. _Who
    Replies:
    7
    Views:
    2,660
Loading...

Share This Page