简体   繁体   English

Ajax发布到codeigniter控制器并给出404,但将其传递给控制器​​?

[英]Ajax post to codeigniter controller giving 404, but making it to the controller?

This is working perfect for me and my team locally, but once we pull it to the live server and try it, it gives me a 404 error but still provides the error response from the login function in the controller. 对于我和我的本地团队来说,这是完美的工作,但是一旦我们将其拉入实时服务器并进行尝试,它会给我一个404错误,但仍然会提供控制器中登录功能的错误响应。 I've been trying to figure this out for several hours now with no luck, and other posts haven't helped. 我已经尝试了好几个小时了,但是没有运气,其他帖子也无济于事。 I'm hoping it's something stupidly simple that I'm not seeing. 我希望这是我没有看到的非常简单的事情。

Edit: The most important part is that post data doesn't get sent on the server, but does locally. 编辑:最重要的部分是发布数据不会在服务器上发送,而是在本地发送。

Ajax call: Ajax呼叫:

    var username = $('#loginUsername').val();
    var password = $('#loginPassword').val();

    var data = {
        "username": username,
        "password": password
    };

    $.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>login",
        data: data,
        success: function(response) {
            //response code
        }
    });

Controller function being called: 控制器功能被调用:

public function login() {
    $data = $_POST;

    if(!$data) {
        //error code
    } else {
        //success code
    }
}

And if it helps, here's the .htaccess: 如果有帮助,这里是.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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>
    ErrorDocument 404 /index.php
</IfModule>

If it running good on localhost that means may be your controller name you writing having case sensitive problem. 如果它在localhost上运行良好,则可能是您编写的控制器名称存在区分大小写的问题。 So first check for case sensitive problem than try it url: "<?php echo site_url('controllerName/functionName'); ?>" 因此,请先尝试检查区分大小写的问题,然后再尝试以下url: "<?php echo site_url('controllerName/functionName'); ?>"

I'm a moron. 我是白痴。 Mod_rewrite wasn't enabled on the server. 未在服务器上启用Mod_rewrite。 I thought it was because other functions were working as if they were, probably due to routing. 我认为这是因为其他功能似乎在起作用,这可能是由于路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM