5

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

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i was wondering how to do that, thanks. but this didnt fix the problem
how cani use php code in .js file. so i need home url code for js file ??
PHP is commands to your service. Javascript is run in your browser. Above example defines base_url before your javascript file is loaded.
6
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", 

4 Comments

GET or POST really doesn't matter here as we are not getting or posting any resource here. but the logic is sound . we need success callback function
i totally agree, however unless the user has a specific reason to use GET then POST should be used according to best practices.. especially when developing a REST-ful API like i presume he is doing. +1 for clarifying.
i agree with you , this is best to use for good programming practices.
thanks for your time. I did try success and complete before but neither worked. could the problem be somewhere else besides my own code?
0

Hello You should add the base url to the URL Ajax.

 $.ajax({
  type: "GET", 
  url: <?php echo base_url();?>"register/hello", 

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

Remember enable the helper URL in the Controller!

Cheers

Comments

0
you need to pass the bas_url to ajax file

<script type="text/javascript">

$(document).ready(function(e) {
    var baseurl = <?php echo base_url();?>

    $('#reg_link_rules').click(function(){

        $.ajax({
            url : baseurl+'index.php/register/hello',
            data : '',
            type: "POST",
           success : function(){


            }
        })
        return false;
    })

});

</script>

Comments

0

Sometimes you have do add index.php to the string. Like so:

 $.ajax({
  type: "GET", 
  url: <?php echo base_url();?>"index.php/register/hello", 

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

But it's all about your config file however. It was the mistake in my case. Hope it helps.

Comments

0

You need to ensure two matter

please put your ajax part as url: ?php echo base_url();?>"register/hello", and type: "GET", and also need to check wheter it is routed the url on config/routes.php.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.