CodeIgniter Forums
how to get url from ajax - 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: how to get url from ajax (/showthread.php?tid=29511)



how to get url from ajax - El Forum - 04-12-2010

[eluser]php beginner[/eluser]
I want ajax url from view file in controller for pagination.


how to get url from ajax - El Forum - 04-12-2010

[eluser]theprodigy[/eluser]
you could pass the url in as a parameter, or try using $_SERVER['HTTP_REFERER']; and see if that works (I'm not sure if the second will work. I've never tried with ajax)


how to get url from ajax - El Forum - 04-12-2010

[eluser]pickupman[/eluser]
Just use site_url() function. In jQuery, I've used:
Code:
$.ajax({
  url: "<?php echo site_url('controller/function');?>",
  //the rest of the options...

});



how to get url from ajax - El Forum - 04-12-2010

[eluser]theprodigy[/eluser]
I thought it was the other way around. I thought they wanted to know in the controller, what page it was that called it, not knowing in the view what page to call. Although, I could be wrong


how to get url from ajax - El Forum - 04-12-2010

[eluser]pickupman[/eluser]
Ahh...I read it the wrong way. Passing a param is a good idea, but how are you using the code in the controller? Shouldn't you already know where it is coming from?


how to get url from ajax - El Forum - 04-12-2010

[eluser]theprodigy[/eluser]
not necessarily. If you are using a sub-view type setup (main site template + sub-views to fill content), and have an auto generated table with pagination, you would need to pass the url back to the controller for the pagination class to build the links.


how to get url from ajax - El Forum - 04-12-2010

[eluser]php beginner[/eluser]
yes i am passing url as parameter but i never wants to change base url


how to get url from ajax - El Forum - 04-13-2010

[eluser]Zeeshan Rasool[/eluser]
If your just want to get base url or any specific url and want to use it in js files where you are doing ajax calls then, declare a JS variable at header section of your main.php or index file and assign your value to it.
<html>
<head>
var my_url = '<?=base_url?>/site/';
</head>
...
...