CodeIgniter Forums
I can't send a AJAX request in a codeigniter server - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: I can't send a AJAX request in a codeigniter server (/thread-77327.html)

Pages: 1 2


I can't send a AJAX request in a codeigniter server - kaiserf50 - 08-17-2020

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


RE: I can't send a AJAX request in a codeigniter server - InsiteFX - 08-18-2020

If you want help on this you will need to show your controller code and ajax code.


RE: I can't send a AJAX request in a codeigniter server - includebeer - 08-20-2020

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.


RE: I can't send a AJAX request in a codeigniter server - kaiserf50 - 08-21-2020

(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,


RE: I can't send a AJAX request in a codeigniter server - InsiteFX - 08-21-2020

Try this your url is wrong.

PHP Code:
url"<?= base_url(login/signin');?>"



RE: I can't send a AJAX request in a codeigniter server - jreklund - 08-21-2020

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.


RE: I can't send a AJAX request in a codeigniter server - stlake2011 - 08-24-2020

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.


RE: I can't send a AJAX request in a codeigniter server - svennd - 08-25-2020

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


RE: I can't send a AJAX request in a codeigniter server - valema - 08-27-2020

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...


RE: I can't send a AJAX request in a codeigniter server - kaiserf50 - 11-03-2020

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