$(document).ready(function() {
$(".comment_flag").click(function(){
$(".comment_flag").prop("disabled", "disabled");
$.ajax({
url: "http://www.somesite.net/ajax/comment_flag",
type: "POST",
data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>', "x":"1"},
success: function(){
//Do something
$("comment_flag").prop("disabled");
},
complete: function(){
$("comment_flag").removeProp("disabled");
},
error: function(){
}
});
});
});
I am having some issues with the data line. I have spent quite a few hours of frustration trying to figure out why I cannot echo into it. I have also tried creating php variables like so:
$x = json_encode($this->security->get_csrf_token_name());
$y = json_encode($this->security->get_csrf_hash());
and javascript variables like so:
var x = <?php echo json_encode($this->security->get_csrf_token_name()); ?>;
var y = <?php echo json_encode($this->security->get_csrf_hash()); ?>;
in the page that the external javascript is loaded into (the first box of code).
I cannot seem to make any of this work. I am using codeIgnighter to pass the crsf data. The only way I can seem to send any data is if I write it in there in between quotes. I have also renamed my javascript file to name.js.php to no avail.
The error I am trying to hurdle is a 403 forbidden (crsf token). Any direction would be great. Thank you.
EDIT: I have tried echoing in just a string and that works fine. It appears the issue I am having then is the:
$this->security->get_csrf_token_name();
$this->security->get_csrf_hash();
are not working.
Receive: Uncaught SyntaxError: Unexpected token ILLEGAL // "Fatal error: Using $this when not in object context", when I run it with the php in between the data brackets.