1

After looking at a solution on ci-ajax-csrf-problem I added the following line into the script and it works fine.

var post_data = {
    '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}

inserted into

$.ajax({
        url: '<?php echo base_url()."ajax/test";?>',
        type:'POST',
        dataType: 'json',
        data: post_data, 

Thank you for the help everyone :)

I am new to Ajax/Jquery and was following a guide on Ajax for CodeIgniter from jorge torres to implement a simple ajax call on my website and ran into problems.

I created a Controller Ajax and this is the code snippet.

class Ajax extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function test() {
        $output_string = 'This is a test';  
        echo json_encode($output_string);
    }

    public function test2(){
        $this->load->view('test.php');
    }
}

And this is the view for that controller, its identical to the one from the tutorial except I added loaded the url helper $this->load->helper('url'); on the first line

Here is the snippet for the script code.

The #getdata is a button type and #result_table is a div

$('#getdata').click(function(){
$.ajax({
        url: '<?php echo base_url().'ajax/test';?>',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call
});

I can successfully access localhost.com/codeigniter/ajax/test2 but when I clicked the button, nothing happen.

I tried looking at the page source info and the url is correct

$.ajax({
        url: 'http://localhost/codeigniter/ajax/test',
        type:'POST'
        ....

Accessing localhost/codeigniter/ajax/test directly is also possible and it display the output message.

I am using CodeIgniter 2.1.3 and my localhost is running on XAMPP 1.7.3

Thank you in advance :)

7
  • check your browser console for any erros Commented Mar 11, 2013 at 8:50
  • have you wrapped the jquery code in $(document).ready(function() { ... }); ..? Commented Mar 11, 2013 at 8:53
  • I am recommending you to install firebug or some kind of developer tools.Check the console for the errors. Commented Mar 11, 2013 at 9:01
  • Plz test with it firebug tools. See what happen when you click in button. Commented Mar 11, 2013 at 9:02
  • Thank you for your reply @arun-p-johny , I tried looking at the browser console and it returns a 500 Internal Server Error with an error message "The action you require is not allowed". Commented Mar 11, 2013 at 9:06

5 Answers 5

4

After looking at a solution on ci-ajax-csrf-problem I added the following line into the script and it works fine.

var post_data = {
    '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}

inserted into

$.ajax({
        url: '<?php echo base_url()."ajax/test";?>',
        type:'POST',
        dataType: 'json',
        data: post_data, 

Thank you for the help everyone :)

Sign up to request clarification or add additional context in comments.

Comments

0

Did your .click() event handler works as expected?

$('#getdata').click(function(){
 alert('clicked');
 ........

if no, try to wrapped the jquery code in $(document).ready(function() { ... }); as @Sudhir suggested:

$(document).ready( function() { 
      $('#getdata').click(function(){
      alert('clicked');
      ........

});

if yes, in case you don't know it yet, there's a tool called firebug to check your AJAX request. I thinks there's no problem with your AJAX url. So far, this is my answer. Hope this helps too.

1 Comment

Thanks for the reply, I checked the console using firebug and it returns a 500 Internal Server Error with a message of "The action you have requested is not allowed."
0

Do you have output compression enabled(gzip) in your config.php? If you compress your output it will fail when you use echo and return the 500 server errors.

3 Comments

Thanks for the reply, I checked the config and its disabled
Sorry just remembered another cause was csrf. Try turning it off to test. If it works check here for the workaround stackoverflow.com/questions/7351849/…
As the solution link you gave is quite different, I used a solution from stackoverflow.com/questions/7348383/…
0

I think there is an issue with your ajax call . Should be like this I think :

$(document).ready (function () { 
   $('#getdata').click(function(){
     $.ajax({
        url: '<?php echo base_url()."ajax/test";?>',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call
    });
});

If the firebug console says that The action you require is not allowed, then it may be a CSRF token issue, turn it off in the codeigniter config.

Add this in config file.

$config['csrf_protection'] = FALSE; 

Hope this helps :)

1 Comment

Sorry for the late reply, yes It was caused by CSRF being enabled, I have found a solution and it works fine :) Thank you for you help
0

Have you tried

$(document).ready (function () { 
   $('#getdata').click(function(){
     $.ajax({
        url: '/ajax/test',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call
    });
});

as apposed to <?php base_url; ?>

Have you entered your site url into the CI config?

Comments

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.