【问题标题】:Calling CodeIgniter controller method returns 404调用 CodeIgniter 控制器方法返回 404
【发布时间】:2019-10-06 05:20:58
【问题描述】:

第一次尝试使用 CI,使用 Ajax 调用方法时出现“找不到页面”错误。仔细检查了名字,现在刚刚在海上迷路了。

[page_a.php]
<script>
  function f1() {
    $.ajax({
    type: 'POST',
    url: '<?= site_url("controller_a/method_a") ?>',
    error: function (jqXHR, exception) {
    var msg = '';
         if (jqXHR.status == 0            ) msg = 'Not connected, verify network [000]';
    else if (jqXHR.status == 404          ) msg = 'Requested page not found [404]';
    else if (jqXHR.status == 500          ) msg = 'Internal server error [500]';
    else if (exception    == 'parsererror') msg = 'Requested JSON parse failed';
    else if (exception    == 'timeout'    ) msg = 'Time out error';
    else if (exception    == 'abort'      ) msg = 'Ajax request aborted';
    else                                    msg = jqXHR.responseText;
    alert(msg);
}});}
</script>

[controller_a.php]
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class controller_a extends MY_Controller {
public function method_a() {
    echo '<script>alert(":-)");</script>';
}}

【问题讨论】:

  • 许多可能性..您的控制器真的扩展了自定义基本控制器吗?如果没有,您应该改为扩展 CI_Controller。另外,请记住控制器的文件名和类名都必须以大写字母开头(因此 class Controller_a extends MY_Controller 和 Controller_a.php 将是适当的名称)
  • 谢谢,改成扩展CI_Controller,使用大写,还是一样的错误。
  • 在你的 url 中包含 index.php 如果你没有配置 index.php,打开 google chrome 控制台并显示请求的去向,显示整个 url,访问 url 并检查有什么问题.

标签: codeigniter http-status-code-404


【解决方案1】:
url: "<?php echo site_url('Controller/method')?>",

试试这个保持控制器的第一个字母大写并回显站点网址。

【讨论】:

    【解决方案2】:

    请检查您的 routes.php 文件。该文件中可能还有另一个重定向。 和

    【讨论】:

      【解决方案3】:
      [page_a.php]
      <script>
        function f1() {
          $.ajax({
          type: 'POST',
          url: '<?= site_url("index.php/Controller_a/Method_a") ?>'
      });}
      </script>
      
      [Controller_a.php]
      <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
      
      class Controller_a extends CI_Controller {
      public function Method_a() {
          echo '<script>alert(":-)");</script>';
      }}
      

      尝试了大写、扩展 CI_Controller 和 index.php,但还没有成功。

      我注意到如果我使用

      <?= site_url("Controller_a/Method_a") ?>
      

      在html中,这样在页面加载/渲染时调用,然后找到并调用该方法,只是页面加载后用Ajax/JS动态调用时没有找到。

      在控制台中的地址是

      www.example.com/Controller_a/Method_a
      

      实际地址是

      www.example.com/web/content/application/controllers/Controller_a.php
      

      【讨论】:

        猜你喜欢
        • 2018-11-07
        • 1970-01-01
        • 2019-01-05
        • 1970-01-01
        • 2015-12-06
        • 2018-07-05
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多