Welcome Guest, Not a member yet? Register   Sign In
Where do you put your AJAX stuff?
#1

[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?
#2

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

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


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

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

[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
#6

[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
        }
    }
#7

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

[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
#9

[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!
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB