I have a web service that I invoke from script but that does not need any information stored in cookies. Anytime I make a request to the service, the cookie is sent along with it. I understand that by default cookies are sent with HTTP request, but is there any way at all to override that behavior and not send the cookie?
In a nutshell, I am issuing my request like this:
$.ajax({ type: "POST", cache: false, url: url, data: data, contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { successFunc(response); }, error: function(xhr) { errorFunc(xhr); } });
Basically, ajax request as well as synchronous request sends your document cookies automatically.
Is not possible to prevent browser to send cookies.
More generally, cookies are not required for AJAX. XmlHttpRequest support (or even iframe remoting, on older browsers) is all that is technically required.
ajax docs: By default, requests are always issued, but the browser may serve results out of its cache. To disallow use of the cached results, set cache to false.
Send AJAX requests to cookie-less subdomain on your server. So you app is www.mydomain.com and ajax requests are served from api.mydomain.com which you never set a cookie on. Also a great idea to do this with static files like images etc...
see the "Use Cookie-free Domains for Components" section of http://developer.yahoo.com/performance/rules.html
Another approach would be prior to doing $.ajax:
1. get the cookies from the browser for your domain with javascript (save them in a global variable)
2. delete the cookies for your domain with javascript from the browser
3. do the $.ajax call
4. place the cookies (from the global variable) back in the browser.
If you don't need the cookies from your domain at all just delete them (so skip 1. and 4.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With