CodeIgniter Forums

Full Version: Problem jQuery in CI 2.0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I'm using CI 1.7.2 then the alert on the following script is running normally or perform. and if I use CI 2.0 on the same script then alert ('test') does not appear.

    jQuery('#action').click(function() {
        var myurl = "http://<?php echo $this->config->item('sms_server');?>/test.php?jsoncallback=?";
        var connection_error = true;

        var arr_hp='';
        var arr_message='';

            type : "POST",
            url : "<?=site_url();?>test_list",
            data : "action=do",
            dataType: "json",
            success : function(data){



how to solve this problem ?

El Forum

[eluser]Eric Barnes[/eluser]
You are using POST so check and see if you have CSRF protection turned on. And if you do then you need to add it to the post params:
data : "action=do&ci;_csrf_token="+$.cookie("ci_csrf_token"),

That is using the jquery cookie library.

If that is not the case then use firebug and see what your responses are.

El Forum

thanks for replying my post.

if $ config ['csrf_protection'] = FALSE; this problem SOLVED.

but if I am use with setting:

$ config ['csrf_protection'] = TRUE;
$ config ['csrf_token_name'] = 'csrf_test_name';
$ config ['csrf_cookie_name'] = 'csrf_cookie_name';

and if I click echo '<input type="button" value="Resend" id="action"> id="action">' and I do not use form_open then the problem is not resolved.

please how to solved this problem?

El Forum

[eluser]Eric Barnes[/eluser]
CSRF expects you to pass the cookie data on any post. So like I said you will have to manually add the cookie to the ajax call.

El Forum

whether the value of csrf_token_name in the hidden text will keep its value forever ?

<div class="hidden">
&lt;input type="hidden" name="csrf_token_name" value="123456789abcabcabc" /&gt;

        if ($CI->config->item('csrf_protection') === TRUE)
            $hidden[$CI->security->csrf_token_name] = $CI->security->csrf_hash;

please enlightenment about the value csrf_token_name ??

El Forum

this problem solved with using cookies.

thank you very muach mr. Eric Barnes.

El Forum

I have the same problem but I don't understand how to resolved that.

Can you help me ?

My javascript is :

$.post("create-account", { name: "John", pass: "test" },
   function(data) {
     alert("Data Loaded: " + data);

and the Php
echo 'data received';

The Php is called and executed but, I can not get the response in my javascript

El Forum

hello, I am having the problem too:

wrap.load(project_url + 'lt/adminuzsakovai/show-company-activity',
                    companyID: $(':hidden[name=imones_id]').val(),
                   // csrf_token_name: "action=do&ci;_csrf_token="+$.cookie("ci_csrf_token")
                    csrf_token_name: $(':hidden[name=csrf_test_name]').val()

I am selecting the csrf_token_name from the form which is generated using form_open(). But when I post this value, I cannot access the function, get an error:

"NetworkError: 500 Internal Server Error - http://localhost/autoloadsSVN/lt/adminuzsakovai/show-company-activity"

when I go to this page using browsers adrres bar then I can acces that url. If I disable csrd protection, then it also works.

Why is it not working even when I post the csrf_token_name ?

El Forum

I don't understand about csrf_token_name because I use CI 2.0 and, as far as I know, CI 2.0 made no such cookie. The cookie was csrf_test_name. Here's an example from my work:

echo "$.post('".site_url()."/siplhp/welcome', 'name=michael&csrf;_test_name=".$this->input->cookie('csrf_test_name')."', function(data) {alert(data)});";

if ($this->input->is_ajax_request())
    // If it's a AJAX request
    echo "Your name is '".$this->input->post('name')."'";
    // Otherwise...

Remember, CodeIgniter (CSRF in this case) always expect $this->input->post('csrf_test_name') in every POST data.