Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 ajax post results in 404 error
#1

I am trying to handle a form submit using the JQuery ajax function on localhost xampp. However, the following always results in a 404 page not found error:


Code:
$(document).on("click", ".iconsTop .iconst", function (e) {
    var idfilter = $(this).attr("id");;
    $.ajax({
      type: "POST",
      dataType: "text",
      url: "filter",
      data: { filter_id:idfilter },
      cache: false,
      success: function (data) {
        $('.maincontact').empty().append(data);
      }
    });
  });

Controller:


PHP Code:
class App_home extends BaseController
{
    public function filter()
    {
        echo 'ok';
    }


Router


PHP Code:
use CodeIgniter\Router\RouteCollection;
$routes->setAutoRoute(true);
$routes->get('filter''App_home::filter'); 

After submitting the form, I always get the following error in Google Chrome's page inspector:

Quote:jquery-3.7.1.min.js:2 POST http://localhost/app/public/filter 404 (Not Found)



Quote:404 Controller or its method is not found: Filter::index
Reply
#2

(This post was last modified: 05-21-2024, 10:38 AM by pippuccio76.)

Why there is public in your url ? 
Have you set base_url with public ?

Can you try to change 

$routes->get('filter', 'App_home::filter');

to

$routes->get('/filter', 'App_home::filter');
Reply
#3

(This post was last modified: 05-21-2024, 12:30 PM by Binboy.)

(05-21-2024, 10:28 AM)pippuccio76 Wrote: Why there is public in your url ? 
Have you set base_url with public ?

Can you try to change 

$routes->get('filter', 'App_home::filter');

to

$routes->get('/filter', 'App_home::filter');
Should I delete public?

My baseurl is as:



Code:
public string $baseURL = 'http://localhost:80/';

I change to `$routes->get('/filter', 'App_home::filter');` but there is the same error
Reply
#4

Well, I don't use setAutoRoute(true). So, I manually set my routes not only for $routes->gets but also for $routes->posts. Maybe this could be your problem too ??

Check here on documentation. You can also use match option for both get and post for the same url
I enjoy web design   d-templates.com
Reply
#5

Did you try like this?
PHP Code:
$.ajax({
    url: `<?= route_to('filter') ?>`, 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB