Awais Qarni
Awais Qarni

Reputation: 18006

jquery.ajax in codeigniter

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`

Upvotes: 0

Views: 1115

Answers (2)

Calvin Froedge
Calvin Froedge

Reputation: 16373

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>

Upvotes: 3

Yasser Souri
Yasser Souri

Reputation: 1957

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.

Upvotes: 3

Related Questions