Action with .json does not work anymore ! Please help!

Dear all,

My controller actions were working before, recently I did change something else, did not touch these action. But seems all the AJAX actions dont work anymore.

Web browser debug console returns 404 Not Found error.

Could you please point me some direction that I can check? I am panic right now. Thank you all.

I did check below:

  • in route.php already have:
$builder->setExtensions(['json', 'xml']);
  • in AppController.php already have:
public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');

In Application.php

  public function bootstrap(): void
    {    
        $this->addPlugin('Ajax', ['bootstrap' => true]);

What else do i missed?

Thank you

I found the problem, for some reason, the setExtensions() in routers.php does not allow to use twice, the 1st statement for JSON will not be applied any more

$routes->scope('/', function (RouteBuilder $builder) {

    $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']); 
    $builder->connect('/wfh', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/timesheets/statistic', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/timesheets', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/pages/*', 'Pages::display');

    $builder->setExtensions(['json', 'xml']); //was working before
    
    //$builder->setExtensions(['pdf']); //new added

In the code above, extensions PDF will cause JSON to stop working.

There is setExtensions() and there is addExtensions() :wink:

Thank you Mr. Kevin,

I actually setExtensions all 3 of them in 1 statement, it still work.

Hi @general2000vn I am also using ajax plugin for my project but i am’t getting response i mean i cannot understand… can you show me how to setup controller for ajax?

i create a ajax file then define all ajax method which send request to control but i am getting reponse
{error:’ ‘,success:’ ',content:‘hello world’}

First, this is a completely separate issue, you should start a new question for it. Second, it seems that your controller is working fine and sending a valid response. What’s wrong about it? (Apart from JSON syntax issues that I’m assuming are from you re-typing the data instead of copy-and-pasting it.)

Hello shaan007

My second post is actually what needed to setup AJAX for controller.

If you getting response like that, I think AJAX works, the only thing you need to check is your logic of generating output.

hello @general2000vn

I have fixed already friend now ajax work well fine. I just confused in some response and request but now all fixed
Thank for Reply @general2000vn .