Welcome Guest, Not a member yet? Register   Sign In
Improved Auto Routing not working with AJAX request
#1

Just started a new project with CodeIgniter and after installation with composer I noticed the following warning in Routes.php:
Code:
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps...

So following the suggestion I set: 
Code:
$routes->setAutoRoute(true);
and in Feature.php: 
Code:
public bool $autoRoutesImproved = true;

The default route in Routes.php:
Code:
$routes->get('/', 'Authentication::index');

This is my controller:
PHP Code:
class Authentication extends BaseController {

    public function index(): ResponseInterface {
        ...

        return $this->response
            
->setBody($this->twig->render('login/view.twig'))
            ->setStatusCode(302);
    }

    public function postLogin(): ResponseInterface {
        $authModel = new AuthenticationModel();

        $response $authModel->verifyLogin($_POST['loginUsername'], $_POST['loginPassword']);

        return $this->response
            
->setBody($response)
            ->setStatusCode(200);
    }


When I go to http://localhost:8080 it loads the login page as it should.

I perform an AJAX request on form submit to verify the user credentials so that the latter can log in; but I am getting 404 on the following URL:
http://localhost:8080/authentication/login

This is the AJAX request:
Code:
pageLoginForm.on('submit', function(e) {
        let isValid = pageLoginForm.valid();
        if (isValid) {
            e.preventDefault();

            $.ajax({
                type: 'POST',
                url: _baseUrl + 'authentication/login',
                data: pageLoginForm.serializeArray(),
                success: function (response) {
                    response === 'login' ? window.location.reload() : $('#errorMsg').text(response);
                },
                error: function () {
                    $('#errorMsg').text('An error occurred!');
                }
            });
        }
    });


I added the prefix "post" to my controller method as instructed by the documentation but it's not working.
Am I missing something?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB