2
$(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.

CrazyGrunt
  • 367
  • 2
  • 4
  • 14
  • 1
    Are you in an object context? Can't tell from the code posted (looks like only part of the script which technically is good as per SO question guidelines) –  Feb 15 '16 at 04:05
  • Honestly don't know what object context is. I am loading the javascript externally into the php page that I am running this from. – CrazyGrunt Feb 15 '16 at 04:11
  • 1
    I was about to try to explain and then I remembered [php.net has good explanation already](http://php.net/manual/en/language.oop5.basic.php) don't be turned away by its header. It is a quite good explanation. –  Feb 15 '16 at 04:15
  • 1
    After reading that, [this SO Q/A might help with that error](http://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context) –  Feb 15 '16 at 04:16
  • Okay so basically if I am understanding this right the $this keyword cannot call to the correct function because it is within the script that is external and embedded into the page. I went ahead and tried taking the external script out and copying it between – CrazyGrunt Feb 15 '16 at 04:29
  • I have a feeling it has something to do with codeIgnighter and the way you setup controllers and views, I have it working internally no issues now. Just not in an external .js.php file. – CrazyGrunt Feb 15 '16 at 04:35
  • 1
    Does that external .js.php file have a reference to codeigniter's security module? I'm sure you can find some way to access that module without an object. Or maybe you make a controller to handle your .js.php files so that you can access the `security` (and other modules) automagically by CI (disclaimer: it's late at night here and I have 0 experience with codeigniter) –  Feb 15 '16 at 04:41
  • 1
    Did you tried passing security token from the controller to the JS file ? I mean, you assign security token to a variable and passing it to the view which you access the js file. and try echoing the variable. http://stackoverflow.com/a/12294560/2138577 – John Fonseka Feb 15 '16 at 04:46
  • @Terminus I went ahead and got it placed into a view. It worked! Only thing to figure out now is how to separate these views because my views is setup to add .php to the end of all my files and while I can sue my one .js.php like that I can't load jquery.js like that. Thanks everyone for the other ideas (I did try them, but either they didn't work or I'm just not advanced enough to understand how to put it together with codeIgnighter.) Terminus if you would post an answer saying that I can comment and add detail on it how I changed it. That way I can mark this answered. Thanks. – CrazyGrunt Feb 15 '16 at 05:56
  • 2
    take a look here http://stackoverflow.com/questions/11269439/how-to-include-csrf-from-codeigniter-into-ajax-data – Vinie Feb 15 '16 at 06:22
  • 1
    data: {"security->get_csrf_token_name(); ?>":"security->get_csrf_hash(); ?>", "x":"1"}, try double quotes – safin chacko Feb 15 '16 at 06:31
  • 1
    CrazyGrunt actually, the link @Vinie found describes your exact situation and has a solution that suggests refactoring in order to have your .js be pure .js so that it can get cached by the browser. Take a look and give it a shot (If that's possible for your situation)! –  Feb 15 '16 at 12:32
  • put this ajax code in your view file ... for access the loaded libraries... – Ashish Raj Feb 20 '16 at 12:35

0 Answers0