0

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
  • Have you checked in the logs on your server, what the internal server error actually is? This is always a good idea, if there is an "internal server error" imho
    – ArSeN
    Dec 30, 2015 at 22:01
  • do you really need csrf (on a POST no less) that simply hits a star counter? is the avoidance of damage worth the effort?
    – dandavis
    Dec 30, 2015 at 22:05

2 Answers 2

0

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
  • Wooo Hooo! That worked. Thanks! This is what I was trying to do. I tried this method before but but I kept receiving an error stating that I was missing a colon. The call was successful, the database was updated correctly and using Firebug I can see that the response is correct but the screen values did not update but I think I can figure that out. Thanks again.
    – ReeseB
    Dec 30, 2015 at 22:22
0

In your javascript that fires when the page initially loads, set up like this:

$.ajaxSetup({ headers: { 'X-CSRF-TOKEN' : '<?php echo $token; ?>' } });

The 'X-CSRF-TOKEN' might be different depending on if you changed that name etc.,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.