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) . Btw, I use Codeigniter.
Here is the code :
PHP (Controller)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Game extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
}
function win()
{
$this->session->set_userdata( array('win') => $win);
$win = $this->input->post('win');
}
function lines()
{
$this->session->set_userdata( array('lines') => $lines);
$lines = $this->input->post('lines');
echo $lines++;
}
function wager(){
$this->session->set_userdata( array('wager') => $wager);
$wager = $this->input->post('wager');
}
}
?>
And here is my Ajax -
function lines()
{
var lines = parseInt($('#lines span').html()) + 1;
$.ajax({
type: 'POST',
url: base_url + 'game/lines',
data: { lines: lines },
success:function(response){
if(response <= 8){
$('#lines span').html(response);
return true;
}
else return false;
}
});
}
And in my HTML I call lines function onclick
.I`m using latest xampp as my virtual host.I also have one custom route in CI -
$route['game/(:any)'] = "game/$1";
P.S. base_url is defined in JS as variable.
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'] = '';
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?
My advice is to put this inside your GUI (view.php).
<script>
function lines(){
var lines = parseInt($('#lines span').html()) + 1;
$.ajax({
type: 'POST',
url: '<?php echo 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
- Thank you all for trying to help me.
- 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.
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.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Game extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
}
function win()
{
$this->session->set_userdata( array('win' => $win));
$win = $this->input->post('win');
}
function lines()
{
$this->session->set_userdata( array('lines' => $lines));
$lines = $this->input->post('lines');
echo $lines++;
}
function wager()
{
$this->session->set_userdata( array('wager' => $wager));
$wager = $this->input->post('wager');
}
}
Please use routes as follow :
$route['game/(:any)'] = "game/lines/$1";
and also try to send full base URL to hit ajax request.