tags:

views:

54

answers:

2

Can an AJAX response set a cookie? If not, what is my alternative solution? Should I set it with Javascript or something similar?

Thanks,

Billy.

+2  A: 

Yes, you can set cookie in the AJAX request in the server-side code just as you'd do for a normal request since the server cannot differentiate between a normal request or an AJAX request.

AJAX requests are just a special way of requesting to server, the server will need to respond back as in any HTTP request. In the response of the request you can add cookies.

this. __curious_geek
Keep in mind that whether the cookie will be honored by the HTTP agent is another story.
Franci Penov
@Franci: agreed. But I think the question only makes sense for http clients that support cookie. So all question-asker only wishes to know if cookies can be written in AJAX request that means his UA supports cookies :)
this. __curious_geek
+1  A: 

According to the w3 spec section 4.6.3 for XMLHttpRequest a user agent should honor the Set-Cookie header. So the answer is yes you should be able to.

Strelok