简体   繁体   中英

How to Update Cookie Value in Angular js (or) JQuery

I am Struck in Updating Cookie Value.Here I am using Angular js for scription. and here i need to update existing cookie, but i can't able update that one.

here is my code, Please any one review and suggest me.

$.ajax({
    type : "POST",
    url : appUrl + "/Login",
    data : {
        username : user,
        password : pwd,
        page : "home"
    },
    dataType : 'json',
    success : function(obj) {
        if (obj.direction != "") {
            var myAry = obj.LoginUserDetails[0];
            //alert("The json array is---->>>"+myAry.partnerid);
            setCookie('LoginDetails', JSON.stringify(myAry));

And After some operation i get the cookie like this

var storedAry = JSON.parse(getCookie('LoginDetails'));

and perform some operations

.ajax({
 url : appUrl+ '/EditPartner',
data : data1,                                                                                        
cache : false,                                                          
contentType : false,
processData : false,                                                                                                                                       
type : 'POST',                                                  
success : function(data) {                                                  
var txt = data;                                                 
var obj = eval('('+ txt + ')');                                                 

loginuserdetails = obj.allregdetails[0];// setCookie('LoginDetails',                                                        

After using this cookie now i want to update my cookie, for that i followed following procedure

setCookie('LoginDetails', JSON.stringify(myAry));//but not succedd so try below code


document.cookie = "LoginDetails"+ "="+ escape(JSON.stringify(loginuserdetails));

And after that i tested by getting that cokkie value but not updated

var logindetailsvalues = JSON.parse(getCookie('LoginDetails'));                                                             
console.log("details after updation---->>"+ getCookie('LoginDetails'));                                                             

For angular use angular cookies file ($cookieStore) http://jsfiddle.net/krimple/9dSb2/

var myApp = angular.module('myApp', ['ngCookies']);

myApp.controller('CookieCtrl', function ($scope, $rootScope, $cookieStore) {
    $scope.bump = function () {
        var lastVal = $cookieStore.get('lastValue');
        if (!lastVal) {
            $rootScope.lastVal = 1;
        } else {
            $rootScope.lastVal = lastVal + 1;
        }
        $cookieStore.put('lastValue', $rootScope.lastVal);
    }
});

myApp.controller('ShowerCtrl', function () {
});

other example

<script>
    angular.module('myApp', ['ngCookies']);
    function CookieCtrl($scope, $cookieStore) {
    $scope.lastVal = $cookieStore.get('tab');

    $scope.changeTab = function(tabName){
        $scope.lastVal = tabName;
        $cookieStore.put('tab', tabName);
    };
    }
</script>

For Jquery

use https://github.com/carhartl/jquery-cookie
example code

$.cookie('the_cookie', 'the_value', { expires: 7 });
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
$.cookie('the_cookie'); // reading => "the_value" 
$.removeCookie('the_cookie');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM