Checking to See If a Request is an AJAX request
During my last large project we had several parts of the site that consumed JSON data received using jQuery's $.get() function. For most of the project, we created separate actions each time that we needed to query for JSON data versus sending out HTML data. Deep into the project we found out that you can do a simple check to with Zend_Request::isXmlHttpRequest().
[code language="php"]
// check to see if this is an AJAX request
if($this->getRequest()->isXmlHttpRequest()){
// Use Zend's JSON helper to make quick work of this request
$this->getHelper('json')->sendJson($arrayData);
}
[/code]
Needless to say we felt stupid for having to duplicate a lot of our actions.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows