codeigniter 中的 jquery.ajax

发布于 2024-11-16 02:52:45 字数 833 浏览 1 评论 0原文

我在 codeigniter 中实现 jquery.ajax 时遇到问题。我想将控件发送到控制器的特定功能。我在我的 javascript 函数中设置 url,如下所示

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

First 是我的控制器,index 是我想要发送控件的函数。当我单击调用它的事件时,会形成以下 url

http://localhost/codeigniter/First/index

该 URL 很好,但会生成 404 错误。我已经在 zendframework 中多次完成此类操作,但无法在 codeigniter 中完成这项工作。我注意到一件事,如果我在网址中添加 index.php ,它就可以正常工作。通过添加index.php,url 变得就像

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

我很惊讶如何从路由文件中删除index.php 一样。我的route.php 文件中只有两行,

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

我已经将控制器设置为默认控制器。 我做的对吗?问题是什么以及如何完成这项工作`

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`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-11-23 02:52:45

您需要检查几件事。首先,在 /application/config/config.php 中确保索引文件设置为:

$config['index_page'] = '';

其次,确保您有正确的 .htaccess。这应该位于您的公共目录的根目录(与您的 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>

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>
¢好甜 2024-11-23 02:52:45

我注意到您的 URL 中缺少的部分是 CodeIgniter 具有的“index.php”内容。
将您的代码更改为:(您需要 URL 帮助程序,因此在此之前加载它):

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

index_page 返回您的网站“索引”页面,如配置文件中指定的那样。

要从 CodeIgniter 链接中删除 index.php,请参阅此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文