CodeIgniter Forums
Setting the filter for ajax queries in the class Filters does not work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Setting the filter for ajax queries in the class Filters does not work (/showthread.php?tid=78010)



Setting the filter for ajax queries in the class Filters does not work - Seva - 11-19-2020

The documentation says that you can customize filters for specific requests (post, get, put etc) class - Filters property - $methods. It is also said that you can do the same for ajax queries and from the console query by setting the key ajax or cli.
But this does not work for the ajax key, I get into the filter if I put the post key. HTTP header 'X-Requested-With': 'XMLHttpRequest' avalaible. isAjax() return true.
What could be the problem?


RE: Setting the filter for ajax queries in the class Filters does not work - InsiteFX - 11-19-2020

Are you sending the 'X-Requested-With' header in your ajax code?


RE: Setting the filter for ajax queries in the class Filters does not work - Seva - 11-20-2020

(11-19-2020, 05:00 PM)InsiteFX Wrote: Are you sending the 'X-Requested-With' header in your ajax code?
Yes, sending this header. In the controller I check with the function isAJAX(), this function return true.


RE: Setting the filter for ajax queries in the class Filters does not work - InsiteFX - 11-20-2020

You need to send that header with JavaScript, not from the controller.

jQuery has a header method for doing this.


RE: Setting the filter for ajax queries in the class Filters does not work - Seva - 11-20-2020

(11-20-2020, 05:43 AM)InsiteFX Wrote: You need to send that header with JavaScript, not from the controller.

jQuery has a header method for doing this.
Here is my code js:
Code:
let button = document.querySelector('input[type=button]');
    button.addEventListener('click', function() {
        ajaxJS(function(res) {console.log(res)});
    });

    
    async function ajaxJS(callback) {
        let data = {'key': 'value'};
        let response = await fetch('ajax', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-Requested-With': 'XMLHttpRequest'
        },
        body: JSON.stringify(data)
        })

        let result = await response.json();
        callback(result);
    }
Then in php controller I check the incoming request with the method:
PHP Code:
public function ajax()
    {
        if (
$this->request->isAJAX()) {
            return 
json_encode(['success'=> 'success']);
        }
    } 



RE: Setting the filter for ajax queries in the class Filters does not work - MGatner - 11-22-2020

https://github.com/codeigniter4/CodeIgniter4/issues/2314


RE: Setting the filter for ajax queries in the class Filters does not work - Seva - 11-28-2020

(11-22-2020, 07:16 AM)MGatner Wrote: https://github.com/codeigniter4/CodeIgniter4/issues/2314
Now it is more or less clear. Thank you for the information