4

I am wondering about how people go about using CodeIgniter and jQuery for AJAX.

In your AJAX request you have

{
  url : ???,
  data : {a:1,b:2},
  success: ....
}

So, how do you build the URL?

Do you

  • have all your JavaScript in your view files, and just use site_url() to build the URL
  • have all your JavaScript in external js files, have a header view you include that has something like <script>var base_url = '<?php echo site_url(); ?>';</script>. Then in your external js files have url: base_url+'rest/of/path/';
  • some other method?

4 Answers 4

4

I have my all my js in an external file and load it in my template.

For specific ajax requests, just call the page as you normally would.

$.ajax({
    type: 'POST',
    url: '/ajax/login',
    data: blabla,
    success: function(data) {
    // do something
    },
    dataType: 'json');
});

In answer to your question, I've had no need to specify the base url, as putting '/' before the controller name sets the root of the site automatically. You could also use ../ etc

Sign up to request clarification or add additional context in comments.

4 Comments

does just using / work when you are developing on localhost where the url is something like http://localhost/myproject/?
Ahh that's a good point. I normally change my hosts file when i develop locally and would set my base_url in config to something like http://local.website.com.. Or you could set up a virtual host within your apache config.
hmm, that is an excellent idea. how would you edit your hosts file? just have local.myproject.com localhost/myproject? or am I completly wrong?
Have a read of this article: apptools.com/phptools/virtualhost.php I would suggest doing something a little simpler like http://myproject (just to save any mistakes). But basically you'll need to create a record in the httpd.conf of apache, then go into your hosts file and point 127.0.0.1 to myproject. That article should help out anyway.
2

if you are writing your ajax in external file then you can define your base url in the view file like

<script>
     var base_url = '<?php echo base_url(); ?>';
</script>

Then on your external ajax file write

url : base_url+'controllerName/functionName',

Comments

0

It can also be done by loading a view that contains your JavaScript.

I currently load JavaScript from a view at the end of the rendered page. Since it is a PHP file with html <script> in it, you can use the URL helper functions like site_url() to generate the URLs you need for each function.

An example view might contain:

<script>
$.ajax{
    url : "<?=site_url("controller/function")?>",
    data : {a:1,b:2},
}
</script>

That will get you CodeIgniter generated URLs for your JavaScript. You could even pass variables into the view for more control over your js.

Comments

0

Usually I make a small javascript in my header, in which I create a base_url and site_url variable (usually being properties of an object I name CI, but that's a personal preeference). I fill these values by echoing the values with PHP. If you make that the first loaded script, you'll always have the site_url available in JS.

Since I'm on mobile I can't post the source now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.