Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skrohnstorgaard's avatar

Laravel request->ajax() not working

Hey people, i just hit a wall that i cant figure out how to breach.. When i want to check if the incoming request is an ajax request using

if ($request->ajax()) {
    // do something
}

I always returns false. When i used jQuery's ajax function it seemed to work fine. The problem i changed the ajax and used this one instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Example_using_XMLHttpRequest

(yes i am avoiding jquery at all costs)

Is there a reason why this script is not detected as an ajax request? I have also tried using

$request->isXmlHttpRequest()

but also without luck.

Thanks in advance!

0 likes
1 reply
nate.a.johnson's avatar

Larvael's Request ajax() method calls the Symphony Request object's method below:

    public function isXmlHttpRequest()
    {
        return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
    }

Check to see if you code is putting that header in the request when it makes an ajax call. You can look at this in Chrome dev tools.

1 like

Please or to participate in this conversation.