Welcome Guest, Not a member yet? Register   Sign In
I can't send a AJAX request in a codeigniter server
#1
Sad 

Hello, everybody.
I'm developing an app in codeigniter 4, the fact is that I have already correctly configured my test server and when I make a request via ajax it returns 404 and "index.php", I have already tried everything and the problem still remains  Huh
Reply
#2

(This post was last modified: 08-18-2020, 11:46 AM by InsiteFX.)

If you want help on this you will need to show your controller code and ajax code.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

With the few information you give, it may be because your ajax request doesn’t call the right URL or there’s an error in your route configuration.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#4

(08-18-2020, 03:47 AM)InsiteFX Wrote: If you want help on this you will need to show your controller code and ajax code.
Here is my code

JavaScript

Code:
$(document).on('submit', '#login', function(e){
    e.preventDefault();
    let datos = {
        usuemail : $("#usuemail").val(),
        usupass  : $("#usupass").val()
    }
    $.ajax({
        url:'/signin',
        method:'POST',
        data:{data: btoa(JSON.stringify(datos))},
        dataType:'JSON',
        beforeSend:function(){
            $("button[type=submit]").addClass('disabled');
            Swal.fire({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                type: 'info',
                title: 'Verficando informacion'
            });
        },
        success:function(response){
            Swal.fire({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                type: 'success',
                title: response.message,
            });
            window.location = '/inicio';
            $("button[type=submit]").removeClass('disabled');
        },
        error: function(request){
            $("button[type=submit]").removeClass('disabled');
            Swal.fire({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                type: 'error',
                title: request.responseJSON.message,
            });
        }
    });
});

Controller

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\Usuarios_model;
use 
CodeIgniter\API\ResponseTrait;

class 
Login extends BaseController{

     use 
ResponseTrait;

    public function 
signin(){
        
$model = new Usuarios_model();
        
$encrypter = \Config\Services::encrypter();
        
$session session();
        if(
$this->request->isAJAX()){
            
$datos json_decode(base64_decode($this->request->getPost('data')));
            
$data['usuemail'] = $datos->usuemail;
            
$data['usupass'] = md5($datos->usupass);
            
$query $model->login_user($data);
            if(
$query->resultID->num_rows 0){
                
$userdata = array();
                foreach(
$query->getResult() as $row){
                    
$userdata['userid']   $row->userid;
                    
$userdata['usupnom']  $row->usupnom;
                    
$userdata['usupape']  $row->usupape;
                    
$userdata['usuemail'] = $row->usuemail;
                    
$userdata['usurol']   intval($row->idrol);
                    
$userdata["deptid"]   $row->deptid;
                    
$userdata['logged']   TRUE;
                }
                
$session->set($userdata);
                return 
$this->respond(json_encode(array('message' => 'Usuario encontrado')), 200);
            }
            else{
                return 
$this->respond(json_encode(array('message' => 'Usuario o contraseña incorrectos')), 404);
            }
        }
        else{
            
redirect()->to('/403');
        }
    } 

Routes.php

PHP Code:
/*Rutas para login*/
$routes->post('/signin''Login::signin'); 


In the development Enviroment works perfectly, but in a Docker container not,
Reply
#5

Try this your url is wrong.

PHP Code:
url"<?= base_url(login/signin');?>"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

If your Docker dosen't work, it dosen't read the .htaccess file to enable SEO-friendly URLs, nothing to do with CodeIgniter. If your urls work with index.php/signin, you could use site_url() to generate said urls. But that would mean all your urls contain index.php just because a miss-configured docker environment.
Reply
#7

(This post was last modified: 08-24-2020, 03:58 PM by stlake2011.)

A little late to the party here but, feeding jreklund's post, if your using apache, make sure your mod_rewrite is enabled.

I was having this problem on an ajax myself and as soon as I looked at my .htaccess file it hit me square between the eyes that was what I forgot to do.
Reply
#8

Totally offtopic, but md5() and passwords are a very bad idea ! https://www.php.net/manual/en/function.md5.php
Reply
#9

Could it matter weather it's a GET or a POST request? I'm quite certain that if you use GET the controller method doesn't need a route...
Reply
#10

hey guys, sorry by the late, I fix it!!, the reason: the httaccess files cannot reading by the apache2, so, i fix the container with a volume, thanks a lot for the help
Reply




Theme © iAndrew 2016 - Forum software by © MyBB