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.
Try changing your routes:
By the way, are you using .htacess?
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:Change it to as below line:
and i am writing your Whole controller without any syntax error replace it with your and it will works.
Please use routes as follow : $route['game/(:any)'] = "game/lines/$1";
and also try to send full base URL to hit ajax request.
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.
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:
In application/config/config.php the index file option should be empty empty like:
My advice is to put this inside your GUI (view.php).
to check your code make the url static like this: