douzao9845 2015-12-30 21:56 采纳率: 0%
浏览 63
已采纳

Codeigniter AJAX JQuery csrf_protection星级评分脚本

I am using the star rating script here but I am using with Codeigniter with CSRF_PROTECTION turned on. I am receiving 500 Internal Server Error when I click on the stars and the script is called. I found a few similar post here but none that helped me solve my issue.

I tried one fix which I found online that stated to create ajaxSetup (see below) function first to merge the "data" with the data in my function to send the token.

I do not know JavaScript so it is taking me days to figure out the issue. The ajaxSetup is not working. If I turn CRSF_PROTECTION off, the script works.

Help! Please, I am struck on this and want to get it to work because there are other Jquery scripts that I would like to use.

$.ajaxSetup({
data: { <?php echo $this->config->item('csrf_token_name'); ?>:
$.cookie('<?php echo $this->config->item('csrf_cookie_name'); ?>') 
}
});

Here is all of the Java script.

<script type="text/javascript">
        $.ajaxSetup({
            data: {
            <?php echo $this->config->item('csrf_token_name'); ?>: $.cookie('<?php echo $this->config->item('csrf_cookie_name'); ?>') 
            }
        });

            $(function() {
                $("#rating_star").codexworld_rating_widget({
                    starLength: '5',
                    initialValue: $('#rating_star').val(),
                    callbackFunctionName: 'processRating',
                    imageDirectory: '<?php echo base_url(); ?>i/icon',
                    inputAttr: 'postID'
                }); 
            });

            function processRating(val, attrVal){
                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url(); ?>rating/rate',
                    data: 'postID='+attrVal+'&ratingPoints='+val,
                    dataType: 'json',
                    success : function(data) {
                        if (data.status == 'ok') {
                            $('#avgrat').text(data.average_rating);
                            $('#totalrat').text(data.rating_number);
                        }else{
                            alert('Some problem occured, please try again.');
                        }
                    }
                });
            }

    </script>
  • 写回答

2条回答 默认 最新

  • doudun3910 2015-12-30 22:12
    关注

    you set default value for data here

    $.ajaxSetup();

    and you are overriding it here

     $.ajax();
    

    so value of token not sending to your server, also you didn't send your data as JSON only you need to send it with with data

    data:{"<?=$csrf['name'];?>":"<?=$csrf['hash'];?>"}
    

    you need to send your data in json format

    data: 'postID='+attrVal+'&ratingPoints='+val,

    to

    data:{"<?=$csrf['name'];?>":"<?=$csrf['hash'];?>", "postID":attrVal, "ratingPoints":val}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)