dongyu9894 2016-05-10 10:16
浏览 521

在jQuery AJAX请求之后创建一个Cookie?

My script is a simple like and dislike buttons (called reactions). It works well but now I want to have a quick cookie created if the vote went through so people can't vote for this particular comment ever again (ie. 1 year.. or when they remove their cookies.)

The entire function works but I am confused on how to add the cookie for each specific comment they have reacted to?

(function( $ ) {
    'use strict';

    $( document ).ready(function() {

        $(document).on('click', '.enco-reaction', function(e){

            var data = {
                'action':       'enco_reaction',
                'reaction':     $(this).data('reaction'),
                'comment':      $(this).data('id')
            };

            $.post( 
                ajax_object.ajax_url,
                data,
                function( obj ) {

                    if( obj.error ) {
                        alert( obj.error );
                    } else {
                        $('#enco-reaction-' + obj.reaction + '-' + obj.comment + ' .enco-reaction-count' ).html( obj.value );
                    }
                },
                'json'
            );
        });

    });     

})( jQuery );
  • 写回答

2条回答 默认 最新

  • dongtan7351 2016-05-10 10:28
    关注

    Best not to use cookies (I would recommend local storage over cookies for storing data as you can store JSON data better and larger) as it's more used for temporary data.

    But for whatever reason you don't want to.

    I'd store data in a JSON Object with a user identifier (however you wish to identifier users from each other) and a list of comments id's.

    {
      "users": {
        "user_ids": {
          "123": [
            "111",
            "222",
            "333"
          ],
          "456": [
            "111",
            "222",
            "333"
          ]
        }
      }
    }
    

    and just JSON.stringify() before storing it in either cookies or local storage.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?