• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax question: Why do I need index.php in the path?

#1
[eluser]shinokada[/eluser]
I have .htaccess with the following.

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|public_html|scripts|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

This and changing routes.php make me to http://127.0.0.1/ci_jquery/jquery/


No index.php.

Now when I use the following jquery, I have to add index.php.

Like index.php/jquery/ajaxprocess instead of /jquery/ajaxprocess.

Why do I need to add index.php?


Code:
$(document).ready(function() {

    // $.AJAX Example Request
    $('#ajaxform').submit(function(eve){
        eve.preventDefault();
        
        $.ajax({
            url: "index.php/jquery/ajaxprocess",
            type: "POST",
            dataType: "html",
            data: $('#ajaxform').serialize(),
            beforeSend: function(){
                showBusy();
            },    
              success: function(html) {
                processForm(html);
             }
        });

    });    ...
....

#2
[eluser]Ben Edmunds[/eluser]
You don't if your htaccess, mod_rewrite, and uri_protocol are setup correctly.

What do you have in config.php for your uri protocol?

Also, is mod_rewrite enabled on your server?


Does http://127.0.0.1/ci_jquery/jquery/someth...than_index work OK in your browser?

#3
[eluser]shinokada[/eluser]
I have $config['uri_protocol'] = "AUTO"; in config.

mod_rewrite is loaded.

And yes, I can see other functions as well.

And thanks to your reply.

I think I sorted out.

If I use $.ajax({
url: "jquery/ajaxprocess",...

instead of
url: "index.php/jquery/ajaxprocess",
or
url: "/jquery/ajaxprocess",

It works. I hope I am doing a right thing.

#4
[eluser]wowdezign[/eluser]
Actually, if I am not mistaken, every single call has to actually go to the index.php file. However, with .htaccess you can remove it from the URL and also redirect every call to the index.php file.

So, two birds with one stone. Every call (legitimate call, that is) must go through the index.php file so it can route, load, etc... On the other hand, index.php in the web address is ugly, confusing, etc...

So by using mod_rewrite you fix both in one file.

Someone correct me if I am wrong, or elaborate if I explained it wrong. I'm not an expert.

#5
[eluser]wowdezign[/eluser]
Quote:If I use $.ajax({
url: “jquery/ajaxprocess”,...

instead of
url: “index.php/jquery/ajaxprocess”,
or
url: “/jquery/ajaxprocess”,

It works. I hope I am doing a right thing.

If you have your configuration set up correctly, you should be able to use:

Code:
<?php echo site_url('jquery/ajaxprocess');?>

site_url() should know all that if you are writing the path from PHP.

If you are trying to use it in a JavaScript file, I usually set a baseURL Javascript variable in the head of my page using:
Code:
[removed]
var baseURL = <?php echo site_url(); ?>;
[removed]

Then inside my js files, I can use that variable to build up absolute URLs.

#6
[eluser]Ben Edmunds[/eluser]
Yes without the forward slash is good. Using site_url() or base_url() is better.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.