Codeigniter Ajax Call 404



我在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');
}
}

每次我得到404错误的Ajax调用和url是正确的。你能帮我一下吗?

谢谢! !

我认为错误是因为你没有返回JSON数据,在你的控制器的功能

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

我认为这将工作:)

将此代码添加到.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文件放入应用程序文件夹,并在下面的文件中添加代码。

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

最新更新