• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prototype Ajax w/ CI Help

#1
[eluser]jaystang[/eluser]
Hey guys,
I'm a bit new to CI and I've been playing around with trying to make an Ajax call to my controller passing a JSON object. Below is the code I've got so far...

Javascript Code:
Code:
var categoryParams = {'categoryId': 123456, 'applicationId': 789123};
var categoryParamsJson = Object.toJSON(categoryParams);

var params = 'params=' . categoryParamsJson;

new Ajax.Request(baseUrl + 'index.php/applications/Applications/testajax/',
{
    method:'post',
    contentType: 'text/json',
    parameters: params,
      onSuccess: function(response)
    {
        alert(response.responseText);
    },
    onFailure: function(){ Alert('failed') }
});

PHP Code:
Code:
public function testajax()
{
    $this->load->helper('json');
    
    $decodedData = json_decode($this->input->post('params'));
    
    echo('hello world from server!' . $decodedData->categoryId);
    die;
}

My issue is with passing an object. Everything works fine if I only pass one integer rather then the object. Any thoughts what I'm doing wrong here?

Thanks.

#2
[eluser]bretticus[/eluser]
Why do you need to call .toJSON() on categoryParams. It's already a JSON object, no?

#3
[eluser]jaystang[/eluser]
I technically don't need it. I had another object I was originally passing through the toJSON() method. I just tried not using that and it's still not working for me.

#4
[eluser]bretticus[/eluser]
So I have a few suggestions. Turn on profiling so you can see what is actually being posted from PHP's perspective. Also, get and install firebug so you can see what is being sent to the ajax url.

Finally, is there any reason why you are setting content type to text/json? It's been awhile since I used Prototype, but it seems like if you send an object as params, that Request turns them into real post variables.

#5
[eluser]jaystang[/eluser]
I actually wasn't sure if i needed the "text/json" stuff or not so I tried it both ways.

Anyway I'm watching everything go across the wire in firebug and I can see it seems to be working (see attached screenshot), but when i profile it on the server side i'm seeing "No POST data exists" so I'm not sure where it's getting lost.

#6
[eluser]jaystang[/eluser]
Ok cancel that, I got it working.

Final working code example...

Javascript Code:
Code:
new Ajax.Request(baseUrl + 'index.php/applications/Applications/testajax/',
{
    method:'post',
    parameters: {'categoryId': 123456, 'applicationId': 789123},
      onSuccess: function(response)
    {
        var result = response.responseText.evalJSON();
        alert(result.catId);
        alert(result.appId);
    },
    onFailure: function(){ Alert('failed') }
});

PHP Code:
Code:
public function testajax()
{
    $this->load->helper('json');
    
    $catId = trim($this->input->post('categoryId'));
    $appId = trim($this->input->post('applicationId'));
    $array = array('catId' => $catId, 'appId' => $appId);
    echo json_encode($array);
    
    die;
}

#7
[eluser]bretticus[/eluser]
Sweet, glad you got it working.

Another good tool to have in your bag for AJAX is firephp. It plugs right into firebug. You basically tell it to log from your PHP code (I use it as a codeigniter lib.)

Cheers!

#8
[eluser]jaystang[/eluser]
Ah great, I will definitely check that out, thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.