CodeIgniter Forums
Where do you put your AJAX stuff? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Where do you put your AJAX stuff? (/showthread.php?tid=24534)



Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Samuurai[/eluser]
Before CI, I used to have one file "ajaxStuff.php" which handled all of my ajax calls.

Now in this MVC architecture of CI, where is the best place to put them? Create a controller just for ajax or within the controller the call relates to?


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Zeeshan Rasool[/eluser]
I just call Ajax calls from my views , but calling it through controller is good option


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Samuurai[/eluser]
Hi Thanks for the reply!


I'm talking about the response to the ajax call. What do you do for that?


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]n0xie[/eluser]
The controller/method that is appropriate for that request?


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Samuurai[/eluser]
Cool... I've got a method called availability, and I've set up another method called availabilityAjax.

I know there's many ways to skin a cat.. I just want to make sure i'm using the best way ! Smile


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]eoinmcg[/eluser]
the way i usually do it is extend the controller class (MY_Controller) to have a method to check if the call was AJAX based.

e.g.
Code:
function _is_ajax()
    {
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
    }

and then in the relevant controller/method i can take appropriate action
e.g.
Code:
function sample_method()
    {
        if($this->_is_ajax())
        {
            // do you ajax thing here    
        }
        else
        {
            // not an ajax call, reload the page or whatever is needed
        }
    }



Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Samuurai[/eluser]
That is effing brilliant! I had no idea that server variable even existed!


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]eoinmcg[/eluser]
glad to be of help.

now just add a sprinkling of magic jquery dust to the mix and you're good to go Wink


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]Samuurai[/eluser]
jQuery is my new best friend ! I'm still a newbie with jQuery and javascript and am still in "bracket and curly brace hell" but I've still got some cool stuff done with it.

Thanks again!


Where do you put your AJAX stuff? - El Forum - 11-12-2009

[eluser]CI_avatar[/eluser]
i am using jquery too and other javascript files.
you can put your javascript anywhere, but most preferable make a js folder at the root folder
then put them all.

Note: use base_url in including your scripts.