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

LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
 
Robert SchuttConnect With a Mentor Software EngineerCommented:
To convert your data array to the type 'application/x-www-form-urlencoded' (which is what you're sending) you could (as a first step) replace line 20 by:
req.send(postData.join('&'));

Open in new window

0
 
Robert SchuttSoftware EngineerCommented:
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).
0
 
Kyle HamiltonData ScientistAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.