1

I am trying an ajax call in CodeIgniter but confused how to write controller and its function name in url.

$.ajax({              
     type : "POST",
     url  : "<?php echo base_uri(); ?>/application/controllers/controllerName/FunctionName",
});

Here I am writing the whole path but it doesn't seem right. Is this ok or is there a better way? Thanks in advance.

5 Answers 5

3

Actually, you should be able to call it like so:

$.ajax({

    type : "POST",
    url  : "/controllerName/FunctionName",

});

But this depends a little bit on your url structure and whether or not you got rid of index.php in the url structure. In that case the url would be "/index.php/controllerName/FunctionName"

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

Comments

1
     $.ajax({
            type : "POST",
            url  : "<?php echo base_url(); ?>/controllerName/FunctionName"
      });

OR

      $.ajax({
            type : "POST",
            url  : "<?php echo site_url(); ?>/controllerName/FunctionName"
      });

Comments

1

Write some where at the top of the page e.g in the header section the following js code.

NOTE: this code should be above your ajax method

<script type="text/javascript">
    var base_url = "<?php echo base_url(); ?>";
    function site_url(url){
        var bu = "<?php echo base_url(); ?>";
        url = (url)?url:"";
        return bu + "index.php/" + url;
    }
</script>

then you can write your ajax method as follow:

$.ajax({
          type : "POST",
          url  : site_url("controllerName/FunctionName"),

   });

Comments

0

Use like this

url  : "<?php echo base_url(); ?>index.php/controllerName/FunctionName",

Errors in your code

url  : "<?php echo base_uri(); ?>/application/controllers/controllerName/FunctionName",
                          ^1           ^2         ^3
  1. its should be base_url not base_uri
  2. and 3. application folder will not use in URL. It is a file structer. Not belong to URL.

2 Comments

great @Abdulla Nilam almost you respond quickly on most of question ;)
@MuhammadUsman i don't get enough time to come online. Just an visit most of time ;)
0

Its working fine,Because I have not removed index.php till yet.

$.ajax({
          url:"<?php echo base_url(); ?>index.php/ajax/hello",
          type:'post',
          data:{
                  "name":name,
                  "email":email,
                  "phone":phone
                },
	      		success:function(data){
	          $("#display").html(data);
	      }
        });

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.