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

I have .htaccess with the following.

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

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?

$(document).ready(function() {

    // $.AJAX Example Request
            url: "index.php/jquery/ajaxprocess",
            type: "POST",
            dataType: "html",
            data: $('#ajaxform').serialize(),
            beforeSend: function(){
              success: function(html) {

    });    ...

[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 work OK in your browser?

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",
url: "/jquery/ajaxprocess",

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

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.

Quote:If I use $.ajax({
url: “jquery/ajaxprocess”,...

instead of
url: “index.php/jquery/ajaxprocess”,
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:

<?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:
var baseURL = <?php echo site_url(); ?>;

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

[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-2021 MyBB Group.