Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

xhr IE send error

I'm trying to use XHR POST, and all is working well except, well, in IE :(

IE doesn't like this line (Invalid Argument):

xhr.send(data);

here's the whole snippet:

var data = ['perfs='+ navigationStart,  domLoading  ];

sendRequest('request.php', data);
            
            function sendRequest(url, postData) {
                        var req = createXMLHTTPObject();
                        if (!req) return;
                        var method = (postData) ? "POST" : "GET";
                        req.open(method,url,true);
                        if (postData)
                                req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
                                req.onreadystatechange = function () {
                                if (req.readyState != 4) return;
                                if (req.status != 200 && req.status != 304) {
                                        return;
                                }
                                window.log(req.responseText);
                        }
                        if (req.readyState == 4) return;
                        req.send(postData);
                }

                

                function createXMLHTTPObject() {
                        var xmlhttp = false;
                        
                        var XMLHttpFactories = [
                                function () {return new XMLHttpRequest()},
                                function () {return new ActiveXObject("Msxml2.XMLHTTP")},
                                function () {return new ActiveXObject("Msxml3.XMLHTTP")},
                                function () {return new ActiveXObject("Microsoft.XMLHTTP")}
                        ];
                        var len = XMLHttpFactories.length;
                        
                        for (var i=0;i<len;i++) {
                                try { xmlhttp = XMLHttpFactories[i]();}
                                catch (e) {continue;}
                                break;
                        }
                        return xmlhttp;
                }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, if it's working OK when you use XMLHttpRequest (in browsers like FireFox and Chrome presumably), then of course by all means, only use the 'old school conversion' to a string when using one of the ActiveX objects (in IE).
Avatar of Kyle Hamilton

ASKER

Awesome - thank you. Did the trick!

I ended up doing this:

req.send(postData.join());

which gives me the standard comma, which I later use to explode the string in php.