0

I am having a problem in implementing jquery.ajax in codeigniter. I want to send the control to a specific function of a controller. I am setting the url in my javascript function like this

var url='<?php echo('First/index');?>';
var ajaxoptions={url:url,success:submit_ajax_response};

First is my controller and index is my function where I want to send the control. When I click on the event on which it is called the following url is formed

http://localhost/codeigniter/First/index

The URL is fine but it is generating the error of 404. I have done such kind of operations various times in zendframework but unable to accomplish this job in codeigniter. I have noticed one thing that if I add index.php in the url it works fine. By adding index.php the url becomes like

http://localhost/codeigniter/index.php/First/index

I am astonished how to remove index.php from route file. I have only two lines in route.php file

$route['default_controller'] = "First";
$route['404_override'] = '';

I have already made my controller as the default controller. Am I doing correct? What is the problem and how to accomplish this job`

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137

2 Answers2

3

I notice the part that is missing form your URL is the "index.php" thing that CodeIgniter has. Change your code to this: (You need URL helper, so load that before this):

var url="<?php echo (index_page() . 'First/index');?>";
var ajaxoptions={url:url,success:submit_ajax_response};

index_page returns your site "index" page, as specified in your config file.

In order to remove index.php from your CodeIgniter links see here.

Community
  • 1
  • 1
Yasser Souri
  • 1,967
  • 2
  • 19
  • 26
  • He should not have to add index.php to the route to get it to work unless he has not removed index.php from route. – Calvin Froedge Jun 20 '11 at 05:19
  • @Calvin Froedge: that's why I echoed index_page(). If he has removed the index.php with mod_rewrite he must have changed `$config['index_page']` so everything should work in that condition too. – Yasser Souri Jun 20 '11 at 05:20
  • @Calving Froedge how to remove index.php from route file? – Awais Qarni Jun 20 '11 at 05:21
  • @Awais: You remove that with mod_rewrite in your .htaccess file. http://stackoverflow.com/questions/1445385/how-to-remove-index-php-in-codeigniters-path – Yasser Souri Jun 20 '11 at 05:23
  • Change config /application/config/config.php so that this line: $config['index_page'] = 'index.php'; is set to: $config['index_page'] = ''; – Calvin Froedge Jun 20 '11 at 05:23
  • Use the .htaccess that Yasser linked to. – Calvin Froedge Jun 20 '11 at 05:23
  • @Awais: Whether you remove index.php with mod_rewrite or not, in order for your code to just work right, it's a good practice to add index_page() as I said. – Yasser Souri Jun 20 '11 at 05:26
  • @Yasser Souri by adding index.php is creating so many problems. Like if I made a directory at root like `js` for javascript files it creates problem in urls of accessing any file in my generated directory – Awais Qarni Jun 20 '11 at 05:28
  • @Awais: No that is not correct. you can still access all your JavaScripts just fine. – Yasser Souri Jun 20 '11 at 05:31
  • @Yasser Souri I have visited your provided link to remove `index.php` through `.htaccess` but it is still having same problem. I want to remove `index.php – Awais Qarni Jun 20 '11 at 05:44
  • What is the problem? You should do two things. First the .htaccess thing. Second you should change your `$config['index_page']` to empty string. – Yasser Souri Jun 20 '11 at 05:46
  • @Yasser Souri Thannks dude. +1 for your nice explaination – Awais Qarni Jun 20 '11 at 05:51
3

You need to check a couple of things. First, in /application/config/config.php make sure index file is set to this:

$config['index_page'] = '';

Second, make sure you have correct .htaccess. This should be at the root of your public directory (same place as your index.php):

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    Options FollowSymLinks

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Calvin Froedge
  • 16,135
  • 16
  • 55
  • 61