ajax request to another controller - url trouble |
[eluser]dan.syme[/eluser]
js library: mootools my ajax call: Code: window.addEvent('domready', function() { on page load of of http://www.domain.com/index.php/events an ajax call to http://www.domain.com/index.php/sim_groups/get/1 triggers. in firebug however the url returned has the events controller called in. example: http://www.domain.com/index.php/events/i...oups/get/1 my sim_groups controller get() function is: Code: function get() I suspect that the view being loaded is causing the issue but I do need to have the view file intact, so I guess maybe I need to check for an ajax call? ideas? Thanks, Dan
[eluser]WebsiteDuck[/eluser]
Change: Code: var path = <?php echo base_url(); ?> + 'index.php/sim_groups'; Code: var path = '<?php echo site_url(); ?>sim_groups'; Also I wouldn't call a view if you're just returning a json object...just echo the json object. But thats up to you.
[eluser]dan.syme[/eluser]
Hi WebsiteDuck and thanks for your help. That produced a call to the following location: http://www.domain.com/index.php/events/<?php echo site_url(); ?>sim_groups/get/1 my changes below Code: window.addEvent('domready', function() { Also I need the view for other reasons.
[eluser]dan.syme[/eluser]
yep loaded into the events view via standard script include
[eluser]WebsiteDuck[/eluser]
Well there are a few things you can do... You can set a global javascript variable path in your events view before you load your .js file. or... You can make the javascript load through a controller function, and then load it through the events view through that function (instead of "whatever.js" you'd include "/events/whatever") This will let you use php in your js file. or... You can hardcode the path into the js file instead
[eluser]dan.syme[/eluser]
just fyi, i should have included that the call works fine when not using base_url or site_url. if I type the whole address out it works fine. if I use base or site_url it injects the events controller into the url
[eluser]dan.syme[/eluser]
ya, just gonna hard code and move on and come back to it when i have more patience. thanks for your help mate.
[eluser]WebsiteDuck[/eluser]
The reason it injects the events controller into the url is because the path doesn't start with a forward slash / The browser thinks the path is supposed to be relative because it doesn't start with a forward slash, so then it tacks on http://www.domain.com/index.php/events/ before your path. Note that in your first code Code: var path = <?php echo base_url(); ?> + 'index.php/sim_groups'; Which is why you ended up with http://www.domain.com/index.php/events/i...oups/get/1
[eluser]tomcode[/eluser]
You can also use : Code: <base href="<?php echo base_url()" /> in the header of Your HTML file and use relative URL's for all assets. If really required, You still can grab that value in Your scripts. |
Welcome Guest, Not a member yet? Register Sign In |