Why does ajax post request for the controller not work?

Asked

Viewed 217 times

1

I am using codeigniter, I needed to execute a function(parm1) of the model in the view, so as recommended I made a request for the controller, in the controller I read the model and call the function.

I am debugging (error_log), inside the method I call in the request.

[controller]

class BaseController extends CI_Controller {

    public function getAgendaDate()
    {
        error_log('herre');
        $this->load->model('agenda_model');
        $this->agenda_model->getAgendaDate();
    }
}

However, I get status 200 ok in the request, but nothing is printed for my log file, hence I conclude that it is not entering the function. In response, from the request I receive my home.php when I should receive the result of a query. Remember that the post got status 200, but did not enter the method I called.

[view]

$.ajax({
    url: "basecontroller/getagendadate",
    type: "POST", //send it through get method
    data: { 
        date: '02-02-2019'
    },
    success: function(response) {
                   console.log(response);
            },
    error: function(xhr) {
            //Do Something to handle error
            console.log(xhr)
        }
    });

Codeigniter 3.1

Does anyone have any idea what’s going on?

  • Maybe your setup needs to call the index php. before declaring the controller/method (Something like: example.com/index.php/basecontroller/getagendadate). And you cannot conclude that method getAgendaDate() is not being called. What if it is being called and not returning anything? Remember to indicate the IC version you are using.

  • I am working on localhost, http://localhost/project name/index.php/controller/method does not work, I am redirected back to the home page. @Shutupmagda yes I can know, because my error_logs are not printed.

  • No, you can’t. What do you want to do with $this->agenda_model->getAgendaDate();? Where is the exit from this method? Because you don’t show what this method are you doing? If it is returning a array or a object, your debug will serve no purpose pq there’s no way out. The show won’t do what you want it to do, it’ll do what you tell it to do.

2 answers

3

Hello, good morning! You need to call the full path of the URL in the parameter url, within your view, so:

$.ajax({
    url: "<?=site_url('basecontroller/getAgendaDate')?>", // <--- OBSERVE AQUI
    type: "POST",
    data: { 
        date: '02-02-2019'
    },
    success: function(response) {
                   console.log(response);
            },
    error: function(xhr) {
            //Do Something to handle error
            console.log(xhr)
        }
    });
  • It doesn’t work, I was redirected to my home page. In the header the request is going like this http://localhost/Calendar/? basecontroller/getAgendaDate

  • 1

    In fact it is wrong because there is a question mark in the URL, making everything after it to be the name of a GET variable. Check in the file config.php in /application/config if the base URL variable is not erroneous with this ?: $config['base_url'] = 'localhost/calendar/?';

0

It’s easier to use that way

$.post('<?=base_url("basecontroller/getagendadate")?>', {
                parametro1: parametro1,
                parametro2: parametro2
},function(data){
    if(data){
        console.log('ok');
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.