为什么我的AJAX帖子在Codeigniter中不起作用


Why is my AJAX .post not working in Codeigniter?

所以这是我的第一个Codeigniter项目,我即将提交seppuku。

情况如下:

我有一个图片库。当用户将鼠标悬停在其中一个上时,一个sidediv将填充AJAX调用的更多信息。

我就是不能让它工作。我一直在开发工具>网络>XHR:中得到这个

Not Found
The requested URL /ajax/getMoreInfo was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我做错了什么?如果有人想放弃一些"最佳实践",我会洗耳恭听。明确地我已经看到了其他例子,其中ajax参数是通过使用$.post(ajax/getMoreInfo/ID)发送的URL传递的,而不是像我在这里所做的那样作为变量传递的。我完全错了吗?(尽管我不认为这是我404的原因)。

这是我在悬停上调用的JS函数

function showDataWindow(){
    var thisClass = $(this).attr('class');
    var thisIDpos = thisClass.indexOf("id-")+3;
    var thisID = thisClass.substr(thisIDpos, 3);
    /// alert(thisID) <- alerts correctly
    $.post('ajax/getMoreInfo', 
        { ID: thisID },
        function(data) {
            .. do stuff with data

我在/controllers 中有一个名为ajax.php的控制器

<?php
class Ajax extends CI_Controller {
  function __construct() {
      parent::__construct(); 
  }
  public function getMoreInfo()
 {
      $ID = $this->input->post('ID');
      $this->load->model('Artist_model','',TRUE);
      $more_info = $this->Artist_model->get_more_info($ID);
      echo json_encode($more_info);
  }
}

我的模特在/模特。。。

<?php 
class Artist_model extends CI_Model {
function __construct()
{
    parent::__construct();
}
public function get_more_info($ID)
{        
        $query = $this->db->query('SELECT * FROM `NOIRusers` WHERE `UID` = `$ID`');
        if ($query->num_rows() > 0)
        {
           foreach ($query->result() as $result)
           {
                $moreInfo['memberID'] =$result->UID;
                $moreInfo['firstName'] =$result->UFname;
                $moreInfo['lastName'] =$result->ULname;
                $moreInfo['large_image_location'] =$result->large_image_location;
                ..more of these..
           }
        }  
}

谢谢你的帮助!

我认为这是一个htaccess问题。请尝试在.htaccess文件中使用以下内容。

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

你把它添加到routes.php中了吗?

http://codeigniter.com/user_guide/general/routing.html

您需要将其添加到此处,以便CI知道如何使用"ajax"。

如果可以正确传递id,请尝试以下查询。

SELECT * FROM NOIRusers WHERE UID = '".$ID."'";

通常使用代码点火器,如

$this->db->select('*');
$this->db->from('NOIRusers');
$this->db->where('UID',$ID);