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

miladrahimi's avatar

Laravel 5 doesn't behave to AngularJS like AJAX Request

I'm using Laravel 5.0 and AngularJS 1.4 In my app exceptions will be thrown and handled by exception handler. It's seems Laravel responds different if the request is AJAX and I counted on it! But Laravel respond to the AngularJS AJAX requests like traditional HTTP requests, HTML not JSON. And Request::ajax() returns false. How can I set AngularJS or Laravel to solve this?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

When doing AJAX calls, the X-Requested-With header is often set to XMLHttpRequest. Laravel's Request::ajax() method is built on top of a Symfony2 method that simply checks for the presence of this header. In October 2012, Angular.js removed this header because they felt that it was rarely used.

You need to set the X-Requested-With parameter in Angular

$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"
3 likes

Please or to participate in this conversation.