jQuery Form plugin does not send XMLHttpRequest value

Hi all…

Here we are, again.

This will be a quick post, just for warning in a specific case. When you’re using the jQuery Form plugin by malsup.com you are making an AJAX call, but it does not send some variables that identify a XMLHttpRequest (in my case I’m using the plugin with a upload form).

For example, when we call a jQuery AJAX function like…

$(function(){  
    // previous code  
    $.get(url, function(data){  
        // next code  
    });  
});

… it sends, for PHP, the $_SERVER[‘HTTP_X_REQUESTED_WITH’] = ‘XMLHttpRequest’ value as usually. But, if you’re using the AJAX Form plugin, it *won’t send* that. It will be a AJAX request (YES), like $.get but that variable will not sent, just this.

But why it doesn’t send that? Because it uses an iframe to send the data to server, so, it’s the way 🙂

If you’re using PHP Frameworks like Zend Framework (like me) some previous actions helpers to identify an AJAX request won’t work:

// ...  
$this->getRequest()->isXmlHttpRequest();  
$this->_helper->getHelper('AjaxContext'); // and it's own functions won't work  
// ...

So, force the layout helper to disable layout (if you’re using it) and viewRenderer helper to disable the view (or not, in some cases).

// in your action function  
$this->_helper->layout->disableLayout();  
$this->_helper->viewRenderer->setNoRender();

That’s all!

Published by

Junior Grossi

senior software engineer & stutterer conference speaker. happy husband & dad. maintains Corcel PHP, elePHPant.me and PHPMG. Engineering Manager @ Paddle

Leave a Reply

Your email address will not be published. Required fields are marked *