Jayesh Naghera
Jayesh Naghera

Reputation: 339

Remove Cookie From Request Headers of Ajax Except Some Of

Problem: A issue is whatever I have set in a cookie's it all has automatic sent to ajax request.

Expected: I want to send only "token", "userName", "lang" into a ajax header.

Tried: I have tried to convert all cookies to javascript localStorage except "token", "userName", "lang" and it's working fine but for this I want to change in entire system. so I don't like

I want minimum change in my code, It is possible ?

Note: Please don't prefer any third party plugin like (Cookie Monster). Because client don't like to implement any plugin.

My Common Ajax Call. (Haven't set any cookie)

$.ajax({
        url         : ajax_url,
        dataType    : "json",
        type        : method,
        data        : params,
        cache       : false,
        crossDomain : true,
        async       : aSync,
        contentType : 'application/x-www-form-urlencoded',
        headers: {
            "X-Requested-With":"XMLHttpRequest"
        },
        error       : function(data, status, error) {
            (ajaxObject.loader == true) ? unblockUI(loaderWrapper) : "";
            failure_callback(error);
        },
        success     : function(data) {

alert(data);

        }
    });

My Common Method For Set Cookies. (After Login)

function login(returnObject) {
        setCookie("token",returnObject.token,1);
        setCookie("userName",returnObject.userName,1);
        setCookie("lang",returnObject.lang,1);
        setCookie("siteName",siteName,1);
        setCookie("gridMaxWidth",gridMaxWidth,1);
        setCookie("environment",returnObject.environment,1);
        setCookie("version",returnObject.version,1);
        setCookie("typeAheadCharCount",returnObject.typeAheadCharCount,1);
        setCookie("warningApplyAllLimit",returnObject.warningApplyAllLimit,1);
    }

Common Set Cookie Function.

/* Set cookies */
function setCookie(cookieKey, cookieValue, cookieExpiry) {
     if (cookieExpiry != -1) {
         document.cookie = cookieKey + "=" + cookieValue + "; path=/";
     } else {
      document.cookie = cookieKey + "=" + cookieValue + "; expires=Thu, 01 Jan 1970 00:00:01 UTC; path=/";
     }
}

Upvotes: 1

Views: 1771

Answers (1)

mplungjan
mplungjan

Reputation: 177885

Your configuration does not belong in a cookie. It belongs in a sessionStorage for the session and localStorage if the user wants to have the same settings next time they visit

That said, when you set a cookie it WILL be sent with all http requests even images residing on the same server of the code that set the cookie.

You cannot change an Ajax request to have less cookies unless you send it to a different server or path (cookies are only sent to the same origin and path)

If you do not want to send so many cookies, then

  • Use sessionStorage or localStorage for setting values
  • Don't set cookies when not needed and delete them when no longer needed.
  • Change the ajax request to a different server or path than what is in the cookie

Upvotes: 1

Related Questions