2013-08-02 80 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); 
      } 
     }); 
    }); 
});