在Codeigniter中获取Ajax上的error404

时间:2013-05-04 10:50:30

标签: php javascript ajax codeigniter http-status-code-404

我的ajax代码有问题。我试图在使用ajax点击时增加内部数量,但在控制台中不断收到错误 - POST localhost/slots/game/lines 404(未找到)。顺便说一句,我使用Codeigniter。

以下是代码:

PHP(控制器)

 <?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');
    }   
}
?> 

这是我的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;
        }

});
}

在我的HTML中,我致电lines function onclick。我使用最新的xampp作为我的虚拟主机。我在CI中也有一个自定义路由 -

$route['game/(:any)'] = "game/$1";
附: base_url在JS中定义为变量。

6 个答案:

答案 0 :(得分:1)

正如我们在评论中发现的那样,你的问题是url_rewrite。试试这个:

创建一个名为.htaccess的文件并将其放在根文件夹中,其中index.php文件是(注意前面的点)。写下这段代码:

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

在application / config / config.php中,索引文件选项应为空,如:

$config['index_page'] = '';

答案 1 :(得分:0)

尝试更改路线:

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

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

顺便问一下,你在使用.htaccess吗?

答案 2 :(得分:0)

我的建议是将它放在你的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>

检查你的代码使url像这样静态:

http://localhost/game/lines

答案 3 :(得分:0)

  1. 谢谢大家帮我。
  2. 最终解决方案是在ajax网址中提供完整路径,例如url: 'localhost/slots/index.php/game/line'
  3. 它必须是xampp的东西,因为在工作中(我们使用Linux和lighttpd)'echo base_url();'工作得很好,所以我可以像这样编写我的ajax url路径:url: '<?php echo base_url(); ?> + 'game/line

    希望你不会像我一样陷入困境。

答案 4 :(得分:0)

如果问题不是url_rewrite,并且您在问题后的最后评论中告诉您,您得到了500 Internal Server Error,那是由于以下控制器方法中的语法错误:

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

将其更改为以下行:

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

并且我正在编写您的Whole控制器,而没有任何语法错误,将其替换为您,它将起作用。

<?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');
   }   
}

答案 5 :(得分:0)

请使用以下路线: $ route ['game /(:any)'] =“游戏/线/ $ 1”;

,并尝试发送完整的基本URL来满足ajax请求。