Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter, Ajax call does not get into Controller
    primarykey
    data
    text
    <p>After looking at a solution on <a href="https://stackoverflow.com/questions/7348383/codeigniter-ajax-csrf-problem">ci-ajax-csrf-problem</a> I added the following line into the script and it works fine.</p> <pre><code>var post_data = { '&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;' : '&lt;?php echo $this-&gt;security-&gt;get_csrf_hash(); ?&gt;' } </code></pre> <p>inserted into </p> <pre><code>$.ajax({ url: '&lt;?php echo base_url()."ajax/test";?&gt;', type:'POST', dataType: 'json', data: post_data, </code></pre> <p>Thank you for the help everyone :)</p> <p>I am new to Ajax/Jquery and was following a guide on Ajax for CodeIgniter from <a href="http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/" rel="nofollow noreferrer">jorge torres</a> to implement a simple ajax call on my website and ran into problems.</p> <p>I created a Controller Ajax and this is the code snippet.</p> <pre><code>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-&gt;load-&gt;view('test.php'); } } </code></pre> <p>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</p> <p>Here is the snippet for the script code.</p> <p>The #getdata is a button type and #result_table is a div</p> <pre><code>$('#getdata').click(function(){ $.ajax({ url: '&lt;?php echo base_url().'ajax/test';?&gt;', type:'POST', dataType: 'json', success: function(output_string){ $('#result_table').append(output_string); } // End of success function of ajax form }); // End of ajax call }); </code></pre> <p>I can successfully access <em>localhost.com/codeigniter/ajax/test2</em> but when I clicked the button, nothing happen.</p> <p>I tried looking at the page source info and the url is correct</p> <pre><code>$.ajax({ url: 'http://localhost/codeigniter/ajax/test', type:'POST' .... </code></pre> <p>Accessing <em>localhost/codeigniter/ajax/test</em> directly is also possible and it display the output message.</p> <p>I am using CodeIgniter 2.1.3 and my localhost is running on XAMPP 1.7.3</p> <p>Thank you in advance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload