Welcome Guest, Not a member yet? Register   Sign In
Setting the filter for ajax queries in the class Filters does not work
#1

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?
Reply
#2

Are you sending the 'X-Requested-With' header in your ajax code?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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.
Reply
#4

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

jQuery has a header method for doing this.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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']);
        }
    } 
Reply
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB