Nadeem
Nadeem

Reputation: 261

How to write CodeIgniter controller URL in jQuery ajax call

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.

Upvotes: 1

Views: 17009

Answers (5)

Ashwani Garg
Ashwani Garg

Reputation: 1527

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

Upvotes: 0

Tanzil Rahman
Tanzil Rahman

Reputation: 11

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"),

   });

Upvotes: 1

Ankit vadariya
Ankit vadariya

Reputation: 1263

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

OR

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

Upvotes: 1

Serg Chernata
Serg Chernata

Reputation: 12400

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"

Upvotes: 3

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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.

Upvotes: 0

Related Questions