K.K. Smith
K.K. Smith

Reputation: 992

Why is my AJAX .post not working in Codeigniter?

So this is my first Codeigniter project, and I'm about to commit seppuku.

Here is the situation:

I have a gallery of pictures. When the user hovers over one of them a side div populates with more info called in by AJAX.

I just can't get it to work. I keep getting this in Dev Tools>Network>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.

What am I doing wrong? and if anyone wants to drop some 'best practices', I'm all ears. Specifically.. I have seen other examples where ajax parameters are passed via the URL sent with $.post (ajax/getMoreInfo/ID) and not as a variable as I do here. Am I completely wrong? (Even though I dont think that is the cause of my 404).

Here is my JS function called on hover

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

I have a controller named ajax.php in /controllers

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

And my model in /models...

<?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..
           }
        }  
}

Thanks for any help!

Upvotes: 1

Views: 2380

Answers (3)

jai
jai

Reputation: 547

please try this following query if you can pass the id properly.

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

normally codeigniter use like

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

Upvotes: 0

sushil
sushil

Reputation: 2671

I think its an htaccess issue. Try using the following content in your .htaccess file.

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

Upvotes: 2

Brian
Brian

Reputation: 8616

Have you added it to the routes.php?

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

You will need to add it in here so CI knows what to do with 'ajax'.

Upvotes: 1

Related Questions