How can I set a cookie in react?

后端 未结 7 2198
忘了有多久
忘了有多久 2020-11-30 20:09

Orginally, I use the following ajax to set cookie.

function setCookieAjax(){
  $.ajax({
    url: `${Web_Servlet}/setCookie`,
    contentType: \'application/         


        
相关标签:
7条回答
  • 2020-11-30 21:02

    You can use default javascript cookies set method. this working perfect.

    createCookieInHour: (cookieName, cookieValue, hourToExpire) => {
        let date = new Date();
        date.setTime(date.getTime()+(hourToExpire*60*60*1000));
        document.cookie = cookieName + " = " + cookieValue + "; expires = " +date.toGMTString();
    },
    

    call java scripts funtion in react method as below,

    createCookieInHour('cookieName', 'cookieValue', 5);
    

    and you can use below way to view cookies.

    let cookie = document.cookie.split(';');
    console.log('cookie : ', cookie);
    

    please refer below document for more information - URL

    0 讨论(0)
提交回复
热议问题