1

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;

1
  • Try control/login in the url Commented Jun 27, 2016 at 8:08

5 Answers 5

1

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

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

Comments

1

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

Comments

0

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.

Comments

0
    $.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

Comments

0

Try this:

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

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.