Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何从视图调用codeigniter控制器方法_Jquery_Ajax_Html_Codeigniter - Fatal编程技术网

Jquery 如何从视图调用codeigniter控制器方法

Jquery 如何从视图调用codeigniter控制器方法,jquery,ajax,html,codeigniter,Jquery,Ajax,Html,Codeigniter,我知道在CI视图中,如果我将 hostname/projectname/controllername/methodname 它将调用CI控制器的方法 但这不起作用。它说请求url不存在。但是如果我说像, hostname/projectname/index.php/controllername/methodname 它起作用了。 问题出在哪里?您需要配置.htaccess文件以删除需要的index.php 如果您还没有.htaccess文件,那么 在CodeIgniter中,您希望.htac

我知道在CI视图中,如果我将

hostname/projectname/controllername/methodname
它将调用CI控制器的方法

但这不起作用。它说请求url不存在。但是如果我说像,

hostname/projectname/index.php/controllername/methodname
它起作用了。
问题出在哪里?

您需要配置.htaccess文件以删除需要的index.php

如果您还没有.htaccess文件,那么

在CodeIgniter中,您希望.htaccess文件如下所示:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Code Igniter Updates

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

    ###

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # 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>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

重新启动发动机
重写基/
###代码点火器更新
#如果您的默认控制器不是
#“欢迎”您可能应该更改此选项
重写规则^(欢迎(/index)|索引(\.php)/?$/[L,R=301]
重写规则^(.*)/索引/?$$1[L,R=301]
#删除尾部斜杠(防止SEO重复内容问题)
重写cond%{REQUEST_FILENAME}-D
重写规则^(+)/$$1[L,R=301]
#强制执行www
#如果您有子域,可以将它们添加到
#使用“|”(或)正则表达式运算符的列表
重写cond%{HTTP_HOST}^(www |子域)[NC]
重写规则^(.*)$http://www.domain.tld/$1[L,R=301]
#不强制执行www
#重写cond%{HTTP_HOST}^www[NC]
#重写规则^(.*)$http://domain.tld/$1[L,R=301]
###
#删除用户对系统文件夹的访问权限。
#此外,这将允许您创建System.php控制器,
#以前这是不可能的。
#如果已重命名系统文件夹,则可以替换“系统”。
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$/index.php/$1[L]
#检查用户是否试图访问有效文件,
#例如图像或css文档,如果这不是真的,它会发送
#请求index.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]
#在没有mod_重写的情况下,将404路由到前控制器
ErrorDocument 404/index.php

将:
www.domain.tld
替换为您的域,例如测试期间的本地主机。

将其添加到您的
htaccess文件中

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
提及

更新完整代码

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

重新启动发动机
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D

您需要在服务器上安装
mod_rewrite
,并且您需要根据需要编辑您的
.htaccess
仍然无法工作:(.是因为我错过了安装mod_rewrite吗?mod_rewrite是apache服务器安装的一部分,因此您可能需要检查您的本地apache安装是否启用了mod_rewrite。我使用phpinfo()进行了检查)我检查了它是否在加载的模块中。这是否意味着它是启用的?如果是,为什么它仍然不工作:(