【问题标题】:external js & css not loading in codeigniter外部 js 和 css 未在 codeigniter 中加载
【发布时间】:2013-03-12 06:35:43
【问题描述】:

要在 codeignitor 中加载外部 Javascript 文件,我正在使用

<script src="<?php echo base_url("application/js/contentslider.js"); ?>" type="text/javascript"></script>

我可以在页面的源代码中看到

<script src="http://localhost/cinifb_ci/application/js/contentslider.js" type="text/javascript"></script>

这意味着据我所知,对于我的 CSS 文件,该文件的加载方式非常相似,我使用过

<link rel="stylesheet" href="<?php echo base_url(); ?>application/css/default.css" type="text/css" />

在页面的源代码中显示为

<link rel="stylesheet" href="http://localhost/cinifb_ci/application/css/default.css" type="text/css" />

这意味着它甚至可以完美加载。 (在单击源中的外部 js/css 链接时,它显示 403 Forbidden)。但是它们的效果都没有应用到页面上。它的显示没有错误。 我已在控制器中将 URL 帮助程序加载为 $this-&gt;load-&gt;helper('url');

并将$autoload['helper'] = array('url'); 放在autoload.php 中

你能解释一下我哪里出错了。

我了解到要加载外部 JS,我们需要安装 jquery-codeigniter 库。我已经下载了它们并将相应的文件包含在我的应用程序文件夹的相应类似文件夹中。

现在当我尝试在我的控制器中包含$this-&gt;load-&gt;library('jquery'); 时,它会显示

An Error Was Encountered

Unable to load the requested class: jquery

因此,我请求您帮助我理解为什么 403 Forbidden 在我可以看到它指向 JS/CSS 文件的确切位置时显示,其次如果 jquery-codeigniter 库的安装有误,请指导我.
注意:http://ellislab.com/codeigniter/user-guide/libraries/javascript.html 没有帮助我

编辑:
jquery-codeigniter 库我放在system/libraries/javascript/

在系统 .htaccess 文件中,我写成

RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|video_files|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /cinifb_ci/index.php/$1 [L]

在我写的视图中

$this->load->helper('url');
<script src="<?php echo base_url("application/js/jquery-1.8.2.js"); ?>" type="text/javascript"></script>

在控制器中

$this->load->helper('url');
$this->load->library('javascript');
$this->load->library('javascript/jquery');
$this->load->library('Jquery_ext');

再次,如果我查看源它指向正确的源,但如果我单击外部 js 文件链接,它仍然显示 403 Forbidden 错误
抱歉,帖子太长了:(
请提出建议..!

【问题讨论】:

  • 你在标题中包含 jquery 文件
  • @DesertP 当然我已经将它们包含在标题本身中
  • 因为我没有得到很好的回复,在我给出实时源 URL 的那一天,当然现在它可以工作了 :)

标签: php codeigniter


【解决方案1】:

403 隐藏错误意味着 Web 服务器正在拒绝对该特定资源的请求。

我假设您正在使用提供的 .htaccess 文件来获取漂亮的 url。

在那一行

RewriteCond $1 !^(index\.php|images|robots\.txt)

让它读起来

RewriteCond $1 !^(index\.php|images|robots\.txt|application/js|application/css)

其实上面的可能是错误的,不管怎样,你的想法是你需要将你的css和js文件夹添加到RewriteCond异常列表中

【讨论】:

  • 谢谢,我的 .htaccess 文件包含 Deny from all 你的意思是改变那行!我查过了,没有结果。
  • 应用程序和系统的.htaccess 页面都只包含Deny from all,没有多余的单词。
【解决方案2】:

最好的方法是创建文件夹assets,然后需要放入css、图片、js等

在此文件夹中保存 js/contentslider.js,最后在您的标题类型中:

<script type="text/javascript" src="<?php echo base_url();?>assets/css/js/contentslider.js"></script>

别忘了,你需要在config.php中设置base_url()。

谢谢

【讨论】:

    【解决方案3】:

    您应该将 js 文件夹包含到您的 .htaccess 中 如果您的 .htaccess 文件在您的应用程序文件夹中,则将其复制并粘贴到应用程序文件夹之外,意思是根目录,然后打开它并使用我提到的过去代码,您只需在第 3 行中使用您的网站名称更改名称 sample 或你的主文件夹

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.php/$0 [PT,L]
    

    更新

    【讨论】:

    • 它抛出了500 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.
    • 根目录下是否有index.php文件?
    • 您的网站或主文件夹名称是什么?
    • 试试这个并告诉我结果 localhost/cinifb_ci/home
    • localhost/cinifb_ci/home & localhost/cinifb_ci/index.php/home 给我Internal Server Error 500 当.htaccess 留空时它至少加载html
    猜你喜欢
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多