brianleroux / xui

A tiny javascript framework for mobile web apps.
http://github.com/xui/xui
391 stars 159 forks source link

xhr 'post' option not setting request header content type #6

Closed OneHQ closed 13 years ago

OneHQ commented 14 years ago

The documenation shows post as a supported method for xhr. Looking at the source code, it doesn't look like the correct request header is set onto the request if post is used. The lack of this header is stopping the XHR post from working correctly.

I chaned my local copy to use the logic below and I was able to successfully use the post option on the xhr() method.

        xhr:function(url,options) {   

            if (options == undefined) var options = {};

            var that   = this;
            var req    = new XMLHttpRequest();

            var method = options.method || 'get';
            var async  = options.async || false ;            
            var params = options.data || null;

                    req.open(method,url,async);
                    req.onload = (options.callback != null) ? options.callback : function() { that.html(this.responseText); }
                    if (method === 'post') {
                        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        req.send(params);
                    } else {
                        req.send(null);
                    }

            return this;
        },
alunny commented 14 years ago

My fork of the repo has the code for any number of headers: http://github.com/alunny/xui/blob/master/src/js/lib/xhr.js

Hopefully it can be pulled when Brian has a chance.

OneHQ commented 14 years ago

Thanks guys, looks like your'e on top of it already. Keep up the great work on this little library.