0

I am confused why codeigniter wont let me use this:

$(function() {
    var csrf = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>"),
        csrfToken = "<?php echo $this->security->get_csrf_token_name(); ?>";
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, {csrfToken: csrf}, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});

When I do the same exact code like this it works fine:

$(function() {
    var csrf = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>");
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, {<?php echo $this->security->get_csrf_token_name(); ?>: csrf}, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});

Why cant I make security->get_csrf_token_name(); ?> into a variable?

3
  • What are the errors? Is token name free of characters that could break the javascript code? Commented Apr 3, 2012 at 7:49
  • On first example you didnt define csrfToken variable. like ` var csrfToken = "...`. Maybe thats why Commented Apr 3, 2012 at 7:54
  • The Error is "The action you have requested is not allowed." Commented Apr 3, 2012 at 8:11

1 Answer 1

3

Because you can't use variables as object keys. You need to insert it like this:

$(function() {
    var postData = {};
    var csrfToken = "<?php echo $this->security->get_csrf_token_name(); ?>";
    postData[csrfToken] = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>"),
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, postData, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks that works great but I have another question if I have another two functions that I would like to be able to call the same info but they have more data they are pushing. Is there a way to make it so I can use it like $.post(url, {username: username, email: email, password: password, postData}, function (data) { I would like to use the csrt info as a constant
$.post(url, $.extend(postData, userData), function(data){}); More on the function $.extend: api.jquery.com/jQuery.extend
So I would have to make: var userData = {}; userData[username] = username;userData[email] = email;userData[password] = password;
No need to. The only thing you can't do is using variables as keys. var userData = {username: username, email: email, password: password}; will work because it's like writing var userData = {"username": username, "email": email, "password": password};

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.