Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax jquery with codeigniter. url doesnt access controller

I am new to codeigniter and cannot get my ajax to work.
I am trying to make links load content into the main document on click.
I looked for instructions but could not figure it out. All works except the ajax returns alert('ERROR') message. nothing is loadded into <div id='load_here'></div>
Maybe I am missing something in config.php? Do i have to load some library for this to work? Any ideas would be helpfull //main document link

<span id='reg_link_rules'>Link</span>  
<div id='load_here'></div>

// controller

class Register extends CI_Controller {
   public function hello()
   {
      echo 'hello';
   }
}

// jQuery

$(document).ready(function() {
    $('#reg_link_rules').click(function(eve){

    $.ajax({
      type: "GET", 
      url: "register/hello", 

      complete: function(data){
        $('#load_here').html(data);
    },
    error: function(){alert('error');}
    });
  });
});

I think the problem is that the ajax url does not access the controller. Z:/home/codeigniter/www/register/test this is where i think it takes me

problem solved, the url needed to be http://codeigniter/index.php/register/hello

like image 915
Semur Nabiev Avatar asked May 01 '12 13:05

Semur Nabiev


2 Answers

Try with url: "/register/hello".

Might do the trick.

Usually I do a

<script type="text/javascript">
    base_url = '<?=base_url()?>';
</script>

At the beginning of my page and simply

base_url+"register/hello"

instead

That makes my ajax more reliable, even when / is incorrect.

like image 139
Robin Castlin Avatar answered Sep 24 '22 02:09

Robin Castlin


complete: function(data){
        $('#load_here').html(data);
    },

should be

Just referencing another SO question ... Use success() or complete() in AJAX call

success: function(data){
        $('#load_here').html(data);
    },

AND

$.ajax({
  type: "GET", 

Should be

unless you want your form vars to be submitted along in the URL.

$.ajax({
  type: "POST", 
like image 26
NDBoost Avatar answered Sep 25 '22 02:09

NDBoost