Best of all, inside the controller, to know if the XMLHTTP request is one or not (ZF)

My other logic in action depends on whether the AJAX request is one or not.
(For AJAX logins, I do not need to redirect after a successful login, which, for example, does not apply to regular logins).
What is the best way besides checking the headers for X-Requested-With: XMLHttpRequest
Is there a flag or something else?

+1
ajax php zend-framework
source share
4 answers

This method works by checking the header, which is set by almost (if not) all major JS libraries, including jQuery and YUI.

 $this->getRequest()->isXmlHttpRequest() //returns true if is XHR 

The method described in detail by smack0007 is guaranteed to be accurate, but the method above is good if the connection is always made using the library that sets the header. It is probably not suitable for a public API.

+6
source

There is no reliable method to tell them apart; browsers use almost the same HTTP code for XMLHttpRequest and regular access.

When using different browsers for custom headers and potential interference with a proxy server, I would not trust the X-Requested-With header to gain access in all cases. (And what all isXmlHttpRequest are actually looking for.)

Instead, I would use a parameter (? Ajax = 1) or another method that generates a unique URL, such as a smack clause.

+3
source

I usually create two entry points for my application: /index.php and /ajax/index.php. They both have a common loader, but in ajax.php I set the FrontController parameter to say that this request is an ajax request.

Then I can just register the Request object.

 if($this->getRequest()->getParam('ajax')) { // Ajax request } else { // Normal request } 
+1
source

The Zend_Controller_Request_Http class has a method called isXmlHttpRequest (), which should tell you if the request is Javascript (ajax).

(Outside of coding practice, but) Probably in your action there would be something like this:

 if($this->getRequest()->isXmlHttpRequest()){ //is ajax } else{ //regular request } 
+1
source

All Articles