CodeIgniter Forums

Full Version: Ajax $.get call and codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]FabioAntunes[/eluser]
here's the deal, I have this code:
Code:
$.get(
  url, //base_url()mycontroller/ajax_get
  {some_data1: data1, some_data2: data2},
  function(responseText){
   $('#ajax').html(responseText);
  }).complete(function() {
   //do monkey business
});

and I have this in my controller
Code:
public function ajax_get(){
if($this->input->is_ajax_request()){
          if($this->input->get ( )){ //I want the ajax GET to be redirected to here
           $something['data1']=$this->input->get('data1');
           $something['data2']=$this->input->get('data2');
          }
          else

           $something= array();
          //with this javascript code I'm getting redirected to here
        
         }elseif($this->input->post ( )){
          //yada yad
          
         }
         else{
          if($this->input->get ( )){ //this one is working if I write the url correctly and press enter. So I think that's not a ROUTES.PHP problem
           $something['data1']=$this->input->get('data1');
           $something['data2']=$this->input->get('data2');
          }else
           $something= array();
          
          
          //yada yada
         }
        }
}

I think the problem is somewhere in the javascript part, I'm using Firebug and in the NET->XHR the request is something like this
GET ajax_get

usually when you do a GET call you have a tab for the parameters,

So in the firebug NET->XHR tab, I should've something like this
GET ajax_get?some_data1=etc?some_data2=etc2

Any ideas?

El Forum

[eluser]CroNiX[/eluser]
Do you have codeigniter set up to use query strings? It's just usually better to use POST unless you can't for some reason. So try just changing your jQuery $.get() to a $.post(). CI's default settings expect POST.

El Forum

[eluser]FabioAntunes[/eluser]
Can you explain me why the post is better?

El Forum

[eluser]CroNiX[/eluser]
Because you don't have to alter settings in CI to get it to work. By default, CI only accepts POST values.
If you want to use GET, you have to set CI up to use GET and query strings, and it doesn't work as well as POST.