Programmer
Programmer

Reputation: 157

How to call a codeigniter function in ajax?

In controller I have a _remap() for routing.

My controller is as follows:

public function _remap($id)
{
    $this->index();
}
public function index()
{
    $this->accesscontrol->can_or_redirect('view', 'translation');
    $this->output->view('translation/language');    
}
function process(Request $request){
       // if(Response::ajax()) return "OK";
        return json_encode(array('ok'));
}

My view is as follows:

$('#lang_choice1').each(function() {
     $('#src_trans_lang').val($("#lang_choice1 option:selected").val());
     var msg = $(this).val();

     $.ajax({
           type: "POST",
           url: '<?=site_url('translation/language/process')?>',
           data: msg,
           success: function(data){ }
     });
     return false;
});

I am trying to call the function process in ajax and its not getting called. How do I need to modify the _remap function to call ajax calls as well?

Upvotes: 0

Views: 1952

Answers (1)

Vijay Sharma
Vijay Sharma

Reputation: 831

Try This

Script part

$('#lang_choice1').each(function () {
      $('#src_trans_lang').val($("#lang_choice1 option:selected").val());
      var msg = $(this).val();

      $.ajax({
        type: "POST",
        url: '<?= site_url('language/process') ?>',
        data: {"msg":msg},
        dataType:"json",
        success: function (data) {
          console.log(data);

        }

      });

      return false;
    });

Controller process function

function process() {
    $data = $this->input->post();
    $result['status'] = "ok";
    $result['response'] = $data;
    echo json_encode(array($result));
    exit(0);
  }

Check response in console

Upvotes: 1

Related Questions