Error 404: Page Not Found in CodeIgniter's Ajax Call

I'm facing an issue with an AJAX call in CodeIgniter. Here is the JavaScript code I am using:

$.ajax({
    method: "POST",
    url: MY_CONSTANT + "login/autentificare",
    data: {
        login_email: login_email,
        login_password: login_password
    },
    success: function(result) {
        result = JSON.parse(result);
        console.log(result);
        if (result.errors) {
            if (result.errors.length > 0) {
                swal.fire({
                    text: result.errors,
                    icon: "error",
                    buttonsStyling: false,
                    confirmButtonText: "Ok, got it!",
                    customClass: {
                        confirmButton: "btn font-weight-bold btn-light-primary"
                    }
                }).then(function() {
                    KTUtil.scrollTop();
                });
            }
        } else {
        }
    }
});

Here is the controller function:

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

class Login extends MY_Controller {
    public function index() {
        $this->load->view('login/login');   
    }
    
    public function autentificare() {
        die('test');
    }
}

Despite having the correct URL, I continually receive a 404 error for the AJAX call. Can anyone assist me with this issue?

Thank you!!

Answer №1

It seems like the issue is arising because your controller function is not returning JSON data.

public function authenticate() {
        return json_encode(['message'=>'success']);
 }

I believe this solution should resolve the error :)

Answer №2

Insert the following code into your .htaccess file and place the file within the application folder location.

DirectoryIndex index.php

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Create a new .htaccess file and place it inside the application folder. Add the following code to the newly created file.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A step-by-step guide on bringing in objects in JavaScript and Node.js

Let's say we have a file called main2.js exports.obj = { x: 10, setX: function(y) { this.x = y; }, getX: function() { return this.x; } }; Now, we also have two other files: abc.js const obj = require("./main2").o ...

The MDX blog was set up to showcase markdown content by simply displaying it without rendering, thanks to the utilization of the MDXProvider from @mdx-js/react within Next JS

I'm currently in the process of setting up a blog using MDX and Next.js, but I've encountered an issue with rendering Markdown content. The blog post seems to only display the markdown content as plain text instead of rendering it properly. If y ...

What is the process for choosing a specific id from a JSON structure?

Is there a way to extract specific data from a JSON format within an Ionic project? What is the process for selecting the ID associated with particular data in a JSON format? And how can I retrieve and display the value linked to the selected product&apos ...

Is it possible for me to determine the quantity of lines present in the text without any

I have a user-input field where individuals can enter information. The input can range from no information at all to as little as a single word or unlimited characters. However, my focus is on handling cases where the text exceeds three lines. To address ...

The object created from winRef.nativeWindow.Razorpay is not a valid constructor

I am currently using Angular 4 for a project and I need to integrate razorpay into the project. While it works perfectly fine in the test and development environments, I encounter an error in a specific environment, the live environment. vendor.cdeb9a87 ...

Utilizing imported components to set state in React

In my project, I created a functional component named Age and imported it into another functional component called Signup. This was done in order to dynamically display different divs on a single page based on the authentication status of the user. By sett ...

The Bootstrap 4 navigation bar fails to be responsive and remains open despite attempts at styling

Having trouble with my navbar functionality... After applying CSS styling, it loses responsiveness and remains open on smaller screens. Here is the code snippet. HTML: <nav class="navbar navbar-expand-sm navbar-dark bg-dark" id="big-bar"> ...

Steps for importing a JavaScript file from Landkit into a Vue project

Currently, I am working on an interesting project and utilizing Vue with Landkit Bootstrap. However, I am encountering issues with the JavaScript behavior of the Landkit component. I usually import the following links in my index.html file, but in the new ...

I am facing a challenging issue with React on my console and finding it difficult to resolve

Below is the code snippet from my ShoppingList.js file, ` function ShoppingList() { return ( <ul className="lmj-plant-list"> {plantList.map(({ id, cover, title, description }) => ( <PlantItem to={"/ ...

The JQuery script functions properly when directly embedded in an HTML file, but fails to work when linked from an external file

While I'm working on some code using JQuery's .hover method, I've encountered an interesting issue. It seems to work fine when written directly in the main HTML file index.html, but for some reason, it fails to execute when written in a Java ...

Steps to set up Feathers instance in Jest environment once

When running Jest tests, I want to utilize only one instance of feathers "app" throughout. This is how I currently import app in each test: const app = require('../../src/app'); describe(`service`, () => { it('registered the service&ap ...

Avoiding line breaks when submitting data through Ajax calls is achievable by using the `.val` method

I've come across multiple articles discussing ways to add line breaks to the .val in jQuery, but unfortunately none of them have worked for me. I attempted using the recommended workaround provided by jQuery: $.valHooks.textarea = { get: function( ...

Handle all link clicks on the webpage

My challenge is that some users are required to utilize a virtual desktop in order to access specific information on my website. However, within the virtual environment, there are links leading to external content that do not function properly. I am seekin ...

What are some methods to prevent cookies from being overridden?

Just beginning my journey in web development. Currently utilizing asp.net Web API and Angular with token authentication. Every time a user logs in, I set the token in a cookie and send it with each request. Everything has been running smoothly so far, bu ...

Utilizing yield (generators) in conjunction with selenium webdriver promises: A comprehensive guide

Trying to harness the power of generators in node 0.11.x to streamline my Selenium tests has proven to be a bit challenging. Despite using the official selenium-webdriver module (ver 2.37.0) and co (ver 2.1.0) for generator creation, I find myself struggli ...

Transferring Element Information to a Bootstrap Modal

Struggling to figure out why the solutions I find are not working for me. It seems like it may be related to my understanding of how elements load in a specific order. I have a common issue - trying to pass data from a button into a modal it triggers. Spe ...

Navigating through a intricate JSON dataset

Hello, I apologize for any language barriers as French is my native tongue. I am reaching out to seek assistance with navigating a complex JSON object using a loop in Javascript, particularly with JOINTJS. I am struggling to access the information I need ...

Using ParsleyJS to activate on form fields dynamically created through AJAX requests

My issue arises from having a form that, when a click event occurs, sends an ajax request to another URL. The response from the server is then appended (a series of form inputs) to the end of the current form. The dilemma I am facing is that Parsley, whic ...

Load different fonts dynamically based on environment configuration

My question is regarding loading multiple fonts from a font.css file in React. I need to make the font path dynamic based on the environment variables. Can anyone suggest a solution for achieving this? I've attempted to create a font.scss file and de ...

Using "this" in callback functions in JS references the current context

Part 1: Is there a way to reference a parent property dynamically from within a callback? Consider the following scenario: var someClass = { htmlClass : 'aFunClass', bindEvents: function(){ jQuery(function(){ alert( th ...