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.

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条