0
votes

I am using the .ajax function for an ajax call in codeigniter , my controller is called control and i have a function in it called Login

but when i'm running this code :

 $.ajax({
        url: "Connection/Login",
        type: "POST",
        data: Login_data,
        success: function () {
            alert('ajax worked');
        }

    });

the console gives me the 404 not found error. And adding index.php does not work.

EDIT : i'm able to acces my function in my controller without mentioning the controller at all, so i can call url : "login" , i don't need to add the controller called connection ?

is this because of my route file? because i need that route file for an organized url and i cannot acces another controller function but the controller setup as default controller

route file :

$default_controller = "Webpages"; $controller_exceptions = array('admin','forums');

$route['default_controller'] = $default_controller; $route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;

5
Try control/login in the urlclaudios

5 Answers

1
votes

Add base_url() to ajax url

var base_url = <?php echo base_url()?>;
$.ajax({
    url: base_url+"Connection/Login",
    type: "POST",
    data: Login_data,
    success: function () {
        alert('ajax worked');
    }

});

better passing base_url from input hidden in view

and also set base_url configuration in codeigniter

1
votes

First Method

Add a baseurl+index.php/controllername/functionname if index.php is hiding then ajax url must be baseurl+/controllername/functionname

Second Method

Also only just use the functionname

Below see the examples:

$.ajax({
    type : 'POST',
    url : 'sendrequest',
    data : { allVals : allVals },
    async : false,
    success : function(html)
              {
                  $(".close").click();
              }
});

In this case my controller function is sendrequest

$.ajax({
    type : 'POST',
    url :  $("#app_base_url").val() + 'index.php/user/activities/activity_calendar_add',
    data : { id : id},
    async : false,
    success : function(data)
              {
                  $(".add_act").html(data);
              }
});

Here this $("#app_base_url").val() may consist of the baseurl

0
votes

First you need to call first the controller then followed by the method.

Example: url: '/control/login'

where control is the name of your controller and login is the name of the method.

0
votes
    $.ajax({
        url: "<?php echo site_url('connection/login')?>",
        type: "POST",
        data: Login_data,
        success: function () {
            alert('ajax worked');
        }
    });

use site_url() for routes and base_url() for assets

0
votes

May i hope this may helping you

$.ajax({ url: "login", type: "POST", data: Login_data, success: function () { alert('ajax worked'); } });