How can achieve different behavior for AJAX requests in Redirection Strategy of BjyAuthorize?

98 Views Asked by At

I've implemented my own RedirectionStrategy so i can redirect the user in the page i want. But in case that an AJAX post was made i want to return a JSON structure to client.

1

There are 1 best solutions below

0
On

One way to detect an AJAX request is to check the HTTP_X_REQUESTED_WITH header to see if it equals xmlhttprequest.

In ZF2 the Request object has a function isXmlHttpRequest() that does this check. Then once you've verified the request is from AJAX you can return a JsonModel.

if ($this->getRequest()->isXmlHttpRequest()) {
    return new JsonModel(array());
}