0

I have installed CodeIgniter in localhost/CodeIgniter and written the same path in config file: $config['base_url'] = 'http://localhost/CodeIgniter/'. Now I need to send AJAX request to http://localhost/CodeIgniter/register/user_register

It's not working when I use url /register/user_register in ajax request but works when I use /CodeIgniter/register/user_register.

I don't want to use /CodeIgniter/ directory every time as I am working on local host because when I will deliver the project it will definitely not be in CodeIgniter directory. So I would have to change this every where I have sent the ajax request.

I'm looking for a way which CodeIgniter provide to avoid using this directory every time and define it once so that I can change it easily when delivering the project.

Not working this way

                        $.ajax({

                        url :  "/register/user_register",
                        type: 'POST',
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function(data){
                            alert(data);
                        }


                        });

But working this way

                        $.ajax({

                        url :  "/CodeIgniter/register/user_register",
                        type: 'POST',
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function(data){
                            alert(data);
                        }


                        });
halfer
  • 18,701
  • 13
  • 79
  • 158
yousaf
  • 55
  • 1
  • 8
  • I am not familiar with CodeIgniter, but is your docroot pointing to the root of your CI project? It is better to point it to a public folder, such as `/www` or `/public` within the project. I agree that `/CodeIgniter` does not belong in the URL. – halfer Feb 06 '17 at 19:30
  • thanks for your concern. Where to check the docroot?can you please guide me? – yousaf Feb 06 '17 at 19:35
  • The docroot is set in your web server, such as Apache. For example if your project folder is `/var/www/` and your CodeIgniter folder is at `/var/www/CodeIgniter`, then `/var/www` is your docroot. It is essentially the top-most folder from your web server's perspective. There are [resources on the web](https://stackoverflow.com/questions/5891802/how-do-i-change-the-root-directory-of-an-apache-server) available to show you how to change this, but it does depend on your operating system and specific Apache config. – halfer Feb 06 '17 at 19:43

1 Answers1

0

You could use a JavaScript variable to store your $config['base_url'] variable client-side. Then use it in your AJAX request like:

$.ajax({
    /*...*/
    url: base_url + "mycontroller",
    /*...*/
});

Set your variable this way :

<head>
    <!-- ... -->
    <script>var base_url = "<?= $config['base_url']; ?>";</script>
    <!-- ... -->
</head>

P.S. in this example, the <?= "..." ?> replace <?php echo "..." ?>

halfer
  • 18,701
  • 13
  • 79
  • 158
Nicolas
  • 7,276
  • 3
  • 17
  • 40
  • Hi Nic I am really thankful for your guidance but Is there any way to put this in config file or define in codeIgniter structure ? – yousaf Feb 06 '17 at 19:42
  • @yousaf, no since your Javascript can't talk directly with your PHP server. But it does not matter since you are printing the value present in your config into a javascript variable, if you change that value it will just print the new value into the variable and it will still works. – Nicolas Feb 06 '17 at 19:43
  • ok.. I understand but the thing is its not working with the complete Url starting from localhost... its working starting from CodeIgniter/... – yousaf Feb 06 '17 at 19:54
  • Normally you are accessing your server directly it should work. Check to make sure you didn't forget a port or something. – Nicolas Feb 06 '17 at 19:57
  • No its just echo statement statement there which is working http://localhost/CodeIgniter/register/user_register when i put this url in browser – yousaf Feb 06 '17 at 20:20
  • Can you set your `base_url` to this ? Will it work ? – Nicolas Feb 06 '17 at 20:31
  • But I found another solution with docroot changing by setting up the virtual host here is the link which helped me [link](https://ailoo.net/2008/07/set-up-multiple-virtual-hosts-on-xampp-for-windows/).Thank you very much – yousaf Feb 06 '17 at 21:34