Sunday, February 21, 2010

YUI + Struts2 + asyncRequest + POST

I finally figured out this issue today - working with the YUI 2.x, Struts2. I am using YAHOO.util.Connect.asyncRequest('POST', url, callback, postData), and was finding that the postData was not being set in my action. Turns out - YUI wasn't setting the content type header to "application/x-www-form-urlencoded" and because of this Struts wasn't parsing the post data. The problem was this section of asyncRequest:


if((method.toUpperCase() == 'POST' && this._use_default_post_header) && this._isFormSubmit === false){
this.initHeader('Content-Type', this._default_post_header);
YAHOO.log('Initialize header Content-Type to application/x-www-form-urlencoded; UTF-8 for POST transaction.', 'info', 'Connection');
}


Turns out, this._isFormSubmit was undefined here, so === false turned out to be false. Changing it to !this._isFormSubmit solved the problem.

2 comments:

Sankar said...

This was great! I was able to figure out why my YAHOO async request kept dropping the postData when it reached the action.

I tried setting the property: <constant name="struts.url.includeParams" value="all" \/> in struts.xml, but didnt seem to help. Your solution worked.

Have you figured out a way to explicitly set the content-type when making XHRs?

blue-j said...

Hi,

I am having similar problems. We are using yui 2.6 and struts 1.2. Client browsers are IE 7, 8 and 9. When the async POST request comes to the action, I don't seem to receive parameters. The problem is this is not happening consistently. The issue is very sporadic.

I tested the _isFormSubmit and it comes false in my development environment. I am still not getting what could be the issue.