【问题标题】:XAMPP, Ajax, CodeIgniter - Getting to know if a user exists in database or notXAMPP、Ajax、CodeIgniter - 了解用户是否存在于数据库中
【发布时间】:2017-04-07 01:58:28
【问题描述】:

我有这个代码,用于登录的网络应用程序。 我在 Windows 操作系统上,使用 CodeIgniter(在 Sublime Text 3 中)和 AJAX 调用。我在 AJAX 调用期间收到 403 Forbidden(对于它的 url)。

现在,我只需在登录表单中输入用户名和密码,获取该信息并通过 AJAX 从 JS 文件发送。现在我只想知道给定的用户是否存在于数据库中,并提醒或关于它的东西(它存在/它不存在)

来自 config.php 的 Base_url:

$config['base_url'] = 'http://localhost/myFolder/';

这里是登录信息(部分):

                          <form action="<?php echo base_url(); ?>index.php/login/enter" method="POST" id="formularioLogin">
                             <div class="form-group row">
                              <label for="userInfo" class="col-md-2 col-sm-12 col-form-label">User:</label>
                              <div class="col-md-10 col-sm-12">
                                <input type="text" class="form-control"  id="userInfo" name="userInfo" placeholder="">
                              </div>
                            </div>

                             <div class="form-group row">
                              <label for="passwordInfo" class="col-md-2 col- sm-12 col-form-label">Password:</label>
                              <div class="col-md-10 col-sm-12">
                                <input type="password" class="form-control"  id="passwordInfo" name="passwordInfo" placeholder="">
                              </div>
                            </div> 

                            <div class="form-group row text-right">
                              <div class="col-md-12 col-sm-12">
                                <button type="button" class="btn btn- primary" id="botonSubmitLogin" onClick="login()">Login</button>
                              </div>
                            </div>
                          </form>

这是带有登录功能的JS文件: 函数登录(){

    if(validacionLogin()){
        $.ajax({
                url: "<?php echo site_url(); ?>.'index.php/login/enter/';?             >",
                type: "POST",
                data: {"userInfo": userInfo, 
                       "passwordInfo": passwordInfo, 
                       },
                dataType: "html",
                cache: false,
                beforeSend: function() {    
                    console.log("Procesando...");
                },
                success: function(data) {
                    if(data == "OK"){
                        alert("Exists");
                        console.log(data);
                        return;
                    }

                    if(data == "ERROR"){
                        alert("Doesn't exist");
                        console.log(data);
                        return;
                    }
                }

        });

        //alert("data passed");
        console.log("User: "+userInfo);
        console.log("Password: "+passwordInfo);
    }else{
        alert("Incorrect");
        console.log("User: "+userInfo);
        console.log("Password: "+passwordInfo);
    }
}

这里是登录的模型(login_model):

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


class Login_model extends CI_Model{

public function __construct(){
}

public function login($user, $password){

    $this->db->where('user', $user);
    $this->db->where('contrasenia', $password);

    $query = $this->db->get('users');

    if($query->num_rows()>0){
        return true;
    }else{
        return false;
    }

}
}

这里是控制器(login.php):

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');


 class Login extends CI_Controller{
 function __construct() {
    parent::__construct();
    $this->load->model('login_model');

  }

 function enter(){
    $usuario = $_POST['userInfo'];
    $password = $_POST['passwordInfo'];

    if(isset($user) || isset($password)){
        if($this->login_model->login($user, $password)){
            $rta='OK';
        }
        else{
            $rta="ERROR";
        }
      }
   }
     function index() {
         $this->load->view('login.php');
     }
 }

注意:我已经有一个包含用户的数据库,并且我还在 autoload.php 和 database.php 中添加了数据库

不过,当我在表单中按“登录”时,我在开发工具中收到此错误:

POST http://localhost/myFolder/index.php/%3C?php%20echo%20site_url();%20?%3E.%27index.php/login/enter/%27;?%3E 404 (Not Found)

但是根据我是否将默认控制器更改为 login.php,我会收到 403(禁止)错误。我对如何前进没有太多想法:/


编辑:我已经尝试了用户 @Nobita 的建议。结果:错误 403 禁止。 EDIT2:我回到拥有它的代码(包括 config.php 中的 index.php 并更改 dafult 控制器以欢迎)并将 ajax url 中的链接硬编码为 --> http://localhost/myFolder/index.php/login/enter/ 结果:jquery.min .js:6 POST http://localhost/myFolder/index.php/login/enter/ 500 (Internal Server Error) 我真的不知道发生了什么:/

【问题讨论】:

  • 你尝试在浏览器上打开你的 php
  • @SachinBahukhandi 嗨!嗯,我想是的,但没有用......我认为它与路径有关,而不是代码,但似乎无法意识到问题出在哪里:(
  • 根据我的观点,从 ajax url 中删除空格,第二种数据类型等于 json。如果不工作,请通知我。
  • 我确定你的控制器有一些问题
  • Ajax url 将是 `url: "login/enter"` 如果需要,表单操作也一样

标签: jquery ajax codeigniter localhost


【解决方案1】:

在您的控制器中,您没有打印数据,只是将数据分配到变量中。

function enter(){
    $usuario = $_POST['userInfo'];
    $password = $_POST['passwordInfo'];

    if(isset($user) || isset($password)){
        if($this->login_model->login($user, $password)){
            $rta='OK';
        }
        else{
            $rta="ERROR";
        }
      }
   }

改成下面,

function enter(){
        $usuario = $_POST['userInfo'];
        $password = $_POST['passwordInfo'];

        if(isset($user) || isset($password)){
            if($this->login_model->login($user, $password)){
                $rta='OK';
            }
            else{
                $rta="ERROR";
            }
          }
          else 
          {  
              $rta="ERROR";
          }

          echo $rta;

       }

而且你的 url 格式也是错误的,

POST http://localhost/myFolder/index.php/%3C?php%20echo%20site_url();%20?%3E.%27index.php/login/enter/%27;?%3E 404 (Not Found)

确保在 config.php 文件中配置以下变量,例如 $config['index_page'] = '';$config['base_url'] = 'http://localhost/Yourfolder/';

您的默认控制器为$route['default_controller'] = 'yourcontrollername'; // not yourcontrollername.php

如果 .htaccess 文件未在 index.php 文件所在的项目文件夹中创建,请注意:不在您的应用程序文件夹中,请创建一个名为 .htaccess 的文件并放入以下数据

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

现在你脚本中的网址是错误的

url: "<?php echo site_url(); ?>.'index.php/login/enter/';?  

改成

url: "<?php echo base_url(); ?>login/enter/",  

现在就试试吧:)

【讨论】:

  • 嗨!我以前尝试过类似的东西,我又试了一遍,但现在控制台说:jquery.min.js:6 POST localhost/myFolder/%3C?php%20echo%20base_url();%20?%3Elogin/…403 (Forbidden) :(
  • 你有没有把脚本里的url改成这样的url:"login/enter/", ?
  • 是的 :( 在 enter 函数中打印结果的部分很有帮助,但我没有意识到那里缺少一个部分 :P 我希望这能奏效,所以我可以尝试推进 :( 跨度>
【解决方案2】:

我也遇到了同样的问题,刚刚找到了一个方法,希望对你也有帮助,虽然有点晚了。 考虑这是我想要的网址“localhost/proyecto/index.php/iniciocontroller/validarUsuario”

$.ajax(
        {
            type:"post",
            url: "validarUsuario", // just the action name
            dataType: 'json', 
            data: { "rut": rut },
            success:function(data)
            {
                alert(data);
            },
            error: function() 
            {
                alert("Invalide!");
            }
        }
    );

【讨论】:

  • 嗨!谢谢!下次我再次处理 Code Igniter 时我会试试这个:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2013-08-31
  • 1970-01-01
  • 2021-12-15
  • 2021-12-19
相关资源
最近更新 更多