2013-08-02 112 views
0

我正在开发一个CodeIgniter的项目,但我用jQuery AJAX发送请求时出现问题。我的默认控制器是:我得到'页面未找到'与Codeigniter和AJAX请求错误

$route['default_controller'] = "test"; 

,这里是我的测试控制器:

if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 

class test extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 
    } 
    public function login_with() { 
     echo "1"; 
    } 
} 

,这里是我的AJAX请求:

$(function() { 
    $('#login_with').click(function() { 
     $.ajax({ 
      type: "post", 
      url: "<?= base_url('test/login_with') ?>", 
      data:"login=1", 
      success:function(ajax_success){ 
       alert(ajax_success); 
      } 
     }); 
    }); 
}); 

终于在这里是我的.htaccess文件:

DirectoryIndex index.php 
RewriteEngine on 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|robots\.txt) 
RewriteRule ^(.*)$ index.php?/$1 [L] 

这里有什么问题?请求发送时,我收到404页未找到错误。

+0

你在哪里把你的Ajax代码。在什么文件? – SasaT

+0

我通常只是这样做:'url:'/ test/login_with''并始终有效。 – SasaT

回答

1

site_url()尝试像

$.ajax({ 
     type: "post", 
     url: "<?= site_url('test/login_with'); ?>", 
     data:"login=1", 
     success:function(ajax_success){ 
      alert(ajax_success); 
     } 
    }); 

并把它放在出口呼应像

public function login_with() { 
    echo "1"; 
    exit; 
} 
+0

我已经尝试过它,但同样的错误发生:/ –

+0

你的控制台重定向url .. ?? – Gautam3164

+0

http:// localhost/test/test/login_with –

1

首先检查你的网站已经mod_rewrite启用,则 检查后,如果你有这样的在你的配置/配置.php

$config['index_page'] = ''

我建议你试试这个htaccess的那么:

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] 
+0

没有必要回声,如果它是像<?= site_url();?> – Gautam3164

+0

哦好吧总是不使用捷径:/对不起eheh – sbaaaang

+0

是啊,当我使用<?=镜头标签:)没有必要问题仍然相同。你可以在这里查一下它; http://www.pvpranks.com/uzuntweet/ –

0

试试这个

$(function() { 
    $('#login_with').click(function() { 
     $.ajax({ 
      type: "post", 
      url: window.location.origin+"/test/login_with", 
      data:"login=1", 
      success:function(ajax_success){ 
       alert(ajax_success); 
      } 
     }); 
    }); 
});