[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

xhr IE send error

Posted on 2012-08-12
3
Medium Priority
?
735 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:Kyle Hamilton
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 38286225
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38286231
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
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 38286248
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article discusses how to implement server side field validation and display customized error messages to the client.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question