1

I have to set a cookie in C# and access it in UI through jQuery. I need to access this cookie throughout the application. I'm able to see the cookie in chrome developer tool : Application - Storage - Cookies but if I try to access as $.cookie("_MyCookie") its undefined.

C#

Response.Cookies.Add(new HttpCookie("_MyCookie") { Value = language });

jQuery

$.cookie("_MyCookie"); 
Pang
  • 9,564
  • 146
  • 81
  • 122
user3771120
  • 85
  • 10
  • Follow following Link .. https://forums.asp.net/t/1635824.aspx?Set+cookie+with+JQuery+and+read+it+code+behind – UJS Jun 15 '17 at 04:02

3 Answers3

0

You can get the cookie value in Jquery but there should you need to specify Path parameter as base path like '/' and expire time after then you can get the value as you have mention above.

Hitesh Gohel
  • 117
  • 4
0

JQuery Code

 <script>
        function ShowCookie() {
            var MyCookie = getCookieValue("MyCookieName");
            alert(MyCookie);
        }

        function getCookieValue(name) {
            cookieList = document.cookie.split('; ');
            cookies = {};
            for (i = cookieList.length - 1; i >= 0; i--) {
                cookie = cookieList[i].split('=');
                cookies[cookie[0]] = cookie[1];
            }
            return cookies[name];
        }
    </script>

C# Code

 Response.Cookies["MyCookieName"].Value = "CookieValue";
Hardik Leuwa
  • 3,282
  • 3
  • 14
  • 28
0

I think You are not plugin Jquery Cookie or if plugin then not valid path or something wrong in it.

are you plugin Jquery Cookie? for example you can visit this link

Urvish Patel
  • 134
  • 15