Skip to content Skip to sidebar Skip to footer

Getting Error404 On Ajax In Codeigniter

I have problem with my ajax code. I`m trying to increase number inside span on click with ajax, but keep getting error in console - POST localhost/slots/game/lines 404 (Not Found)

Solution 1:

As we found out in the comments your problem is the url_rewrite. Try this:

Make a file called .htaccess and place it in root folder, where your index.php file is (notice the dot in front). Write this code:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

In application/config/config.php the index file option should be empty empty like:

$config['index_page'] = '';

Solution 2:

Try changing your routes:

$route['game/(:any)'] = "game/lines/$1";

refer to the codeigniter user_guide to learn more about URI routing.

By the way, are you using .htacess?

Solution 3:

My advice is to put this inside your GUI (view.php).

<script>functionlines(){
    var lines = parseInt($('#lines span').html()) + 1;
    $.ajax({
            type: 'POST',
            url: '<?phpecho base_url()?>game/lines',
            data: { lines: lines },
            success:function(response){
                if(response <= 8){
                    $('#lines span').html(response);
                    return true;
                }
                else return false;
            }

    });
}
</script>

to check your code make the url static like this:

http://localhost/game/lines

Solution 4:

  1. Thank you all for trying to help me.
  2. The final solution was to provide full path in ajax url, something like this url: 'localhost/slots/index.php/game/line'.

It must be something with the xampp, because at work (we use Linux and lighttpd) 'echo base_url();' works just fine, so I can write my ajax url path like this: url: '<?php echo base_url(); ?> + 'game/line.

Hope you won't get stuck as I did.

Solution 5:

If url_rewrite is not your problem and as you have told in last comment after question that you got 500 Internal Server Error then it is because of your syntax errors in controller methods as below:

$this->session->set_userdata( array('lines') => $lines); //wrong -syntax error

Change it to as below line:

$this->session->set_userdata( array('lines' => $lines )); //Right 

and i am writing your Whole controller without any syntax error replace it with your and it will works.

<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
classGameextendsCI_Controller{
   function__construct()
   {
    parent::__construct();
   }
   functionindex(){
   }
   functionwin()
   {
    $this->session->set_userdata( array('win' => $win));
    $win = $this->input->post('win');
   }
   functionlines()
   {
    $this->session->set_userdata( array('lines' => $lines));
    $lines = $this->input->post('lines');
    echo$lines++;
   }
   functionwager()
   {
    $this->session->set_userdata( array('wager' => $wager));
    $wager = $this->input->post('wager');
   }   
}

Post a Comment for "Getting Error404 On Ajax In Codeigniter"