3

When I make request from Postman application to a Laravel Application, the

$request->ajax() is not returning TRUE.

I'm also sending the Accept: application/json header in Postman, Still no luck.

Postman Request Screenshot:

Postman Request Screenshot

Code Screenshot

Code Screenshot

1
  • 1
    please don't put screenshots, instead paste the actual code Commented May 3, 2020 at 14:57

1 Answer 1

9

Add the header X-Requested-With: XMLHttpRequest

The explanation

If you track down the method ->ajax() it goes through the following function:

    /**
     * Returns true if the request is a XMLHttpRequest.
     *
     * It works if your JavaScript library sets an X-Requested-With HTTP header.
     * It is known to work with common JavaScript frameworks:
     *
     * @see https://wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
     *
     * @return bool true if the request is an XMLHttpRequest, false otherwise
     */
    public function isXmlHttpRequest()
    {
        return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
    }

Add the header X-Requested-With: XMLHttpRequest and then $request->ajax() will return TRUE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.