【问题标题】:Codeigniter Ajax Call 404Codeigniter Ajax 调用 404
【发布时间】:2021-01-26 10:16:42
【问题描述】:

我在 codeigniter 中遇到 ajax 调用问题。这是javascript代码:

$.ajax({
    method: "POST",
    url: MY_CONSTANT + "login/autentificare",
    data: {
        login_email: login_email,
        login_password: login_password
    },
    success: function(result) {
        result = JSON.parse(result);
        console.log(result);
        if (result.errors) {
            if (result.errors.length > 0) {
                swal.fire({
                    text: result.errors,
                    icon: "error",
                    buttonsStyling: false,
                    confirmButtonText: "Ok, got it!",
                    customClass: {
                        confirmButton: "btn font-weight-bold btn-light-primary"
                    }
                }).then(function() {
                    KTUtil.scrollTop();
                });
            }
        } else {
        }
    }
});

控制器功能是:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
    public function index() {
        $this->load->view('login/login');   
    }
    
    public function autentificare() {
        die('test');
    }
}

每次我收到 Ajax 调用的 404 错误并且 url 是正确的。 你能帮帮我吗?

谢谢!!

【问题讨论】:

  • 你有没有从url中删除index.php?
  • 是的。 $config['index_page'] = ''; .我现在尝试使用 index_php 并开始工作。如何删除 index.php 表单 URL?谢谢!!
  • 我有添加答案请检查。
  • 在您的 config.php 中,是 $config['csrf_protection'] 设置为 true 还是 false?
  • 这是错误的 csrf_protection。我试过 put true 并得到 403 错误。

标签: javascript ajax codeigniter codeigniter-3


【解决方案1】:

我认为错误的出现是因为您没有在控制器的函数中返回 JSON 数据

public function autentificare() {
        return json_encode(['response'=>'success']);
 }

我认为这会起作用:)

【讨论】:

  • 404 在到达 autentificare() 之前被抛出,否则不会有 404。顺便说一句,不返回,但 echo json_encode()
【解决方案2】:

将此代码添加到 .htaccess 文件中,并将文件放在应用程序文件夹位置。

DirectoryIndex index.php

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

创建一个新的 .htaccess 文件并将其放入 application 文件夹中,并将代码添加到下面给出的文件中。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  

【讨论】:

  • 感谢您的回复,但无法正常工作。同样的 404 错误。以前的 htaccess 是: 要求所有拒绝 拒绝所有
  • 我遇到了同样的错误。你确定这是一个htaccess问题吗?谢谢!!
  • 您放置 .htaccess 文件的位置
  • 在应用程序文件夹中是 htaccess。
猜你喜欢
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-03-31
相关资源
最近更新 更多