简体   繁体   中英

CSRF cookie in Ajax Post

I use CodeIgniter framework and use this function to sort my news-posts

$(".news-list").sortable({
 opacity: 0.6,
 cursor: 'move',
 update: function(event, ui) {
    var order = $(this).sortable("serialize"); //console.log(order);
   $.ajax({
     type: 'POST',
     url: "<?php echo base_url();?>news/sort_posts/", 
     data: ( order ),
     success: function(data){ console.log('Success'); },
     error: function(data){ console.log("Fail"); }
    })
},
      distance: 15 
});

Allthough I have to disable CSRF in config.php to make it work, since the CSRF-cookie is not included.

So, with the order-array I want to send:

<?php echo $this->security->get_csrf_token_name() ?> : '<?php echo $this->security->get_csrf_hash() ?>'

How is this done?

You really just need to grab the value and append it to the data params

<?php
$token = $this->security->get_csrf_token_name() . '=' . $token = $this->security->get_csrf_hash();
?>

var order = $(this).sortable("serialize");
order += "&csrf=<?php echo $token; ?>"

Php:

$token = '&'.$this->security->get_csrf_token_name().'='.$this->security->get_csrf_hash();

jQuery:

order += "<?php echo $token; ?>"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM