Error in Ajax query

0

Good evening,

I think it's a simple question but maybe because of the time it is I can not find it because Ajax fails me in the user login:

My form:

<form id="form-validation" name="form-validation">
   <div class="form-group">
        <input id="validation-email"
               class="form-control"
               placeholder="Nombre de usuario"
               name="login-username"
               type="text"
               data-validation="El usuario introducido no es válido">
   </div>
   <div class="form-group">
       <input id="validation-password"
              class="form-control password"
              name="login-password"
              type="password" data-validation=""
              data-validation-message="Contraseña incorrecta"
              placeholder="Contraseña">
   </div>
   <div class="form-group">
   <div class="checkbox">
       <label>
         <input type="checkbox" name="remember" checked>
          Recordar
       </label>
   </div>
   </div>
   <div class="form-actions">
       <button type="submit" class="btn btn-primary width-150">Iniciar Sesión</button>
   </div>
</form>

Ajax:

$(document).ready(function(){

  $(".form-actions > .btn").click(function(e){

    alert($("#form-validation").serialize());
    var base_url = $('#htmlbaseurl').text();

        $.ajax({
                type: 'POST',
                url: base_url + 'Pages/checklogin',
                data: $("#form-validation").serialize(),
                success:function(e){

                    alert("funciono");

                },
                error:function(e){

                alert("error");
                }
      });
  });

 });

My Codeigniter driver

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

class Pages extends CI_Controller {

    public function __construct(){
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');
        $this->load->library('tank_auth');
    }

    public function view($page = 'home')
    {
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $data['base_url'] = base_url();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }

    public function checklogin(){
        $email = $_POST['login-username'];
        $passwd = $_POST['login-password'];

        $query = $this->db->select('id')->from('users')->where('username', $email)->where('password', $passwd)->get();

        if($query->num_rows() > 0){
            setcookie('c_pr_user2', $query->row()->userId, time()+(86400*30), '/');

            redirect(base_url().'');
        }
        else{
            echo 'Error credenciales';
        }
    }
}
    
asked by Korzan 24.03.2017 в 20:07
source

1 answer

0

What error appears on the console?

apparently the error is in your url to which you send the Ajax request:

 $.ajax({
           type: 'POST',
           url: base_url + 'Pages/checklogin', 
           data: $("#form-validation").serialize(),
           success:function(e){

              alert("funciono");

                },
                error:function(e){

                alert("error");
                }
      });
  });

in the variable you concatenate: base_url

    
answered by 24.03.2017 / 20:24
source