XMLHttp
March 26, 2008 by muralis
Use the XMLHttpRequest Object to Post Data
When using XMLHttpRequest object, there is a way to post data in the same way that an HTML Form is posted. All you need to do is add an HTTP header, and send the data the same way you’d send it on the QueryString.Here’s a JScript example for the client side, that submits form data to another page:
var objHTTP, strResult;
objHTTP = new ActiveXObject(’Microsoft.XMLHTTP’);
objHTTP.open(’POST’,”OtherPage.asp”,false);
objHTTP.setRequestHeader(’Content-Type’,
‘application/x-www-form-urlencoded’);
objHTTP.send(”id=1&user=”+txtUser.value+”&password=”+txtPassword.value);
strResult=objHTTP.responseText; Cheers Kor, I’m currently using Aptana to debug my js. I’m running a
local version of my servlet on Apache Tomcat; it may have been possible
to get around the cross-domain security issue in FF by setting up the
debugger in Aptana to run on the same server. However, this is how to
achieve it with code (and a couple of settings in FF).
First of all, I will answer my question on the posting method. If you
would like to “POST” rather than “GET” then you need to enter only the
URL in the xmlHTTPrequest.open() function. The parameters are to go
into the xmlHTTPrequest.send() function. Before you can place the
parameters in the send function, you must first encode the parameters
as a URL, hence the line:
parameters = encodeURI(parameters);
Since this is the “POST” method, we must also include some information
about the request headers. The length must be set, otherwise you will
get a 411 - length required error, and the connection type must also be
set. This must all happen after the connection is open but before the
data is sent, hence:
http_request.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded”);
http_request.setRequestHeader(”Content-length”, parameters.length);
http_request.setRequestHeader(”Connection”, “close”);
FireFox has extra security settings to help protect the user using the
user agent. So if you would like to debug your js locally and you’re
not communicating with a server in the same location, then you will hit
the cross-domain protection that Mozilla has added. To get around this
you need to first of all enable universal browser read every time you
want to do something with the xmlHTTPrequest. Hence the line:
netscape.security.PrivilegeManager.enablePrivilege(”UniversalBrowserRead”);
Inserted before creating the request, overriding the mime type and before the open function.
This alone is not enough. You now need to get your hands dirty with the FF browser itself.
In the URL type “about:config”, get to “signed.applets.codebase_principal_support” and change its value to true.
I believe this is all you have to do to get around the cross-domain
protection in Mozilla. However, if you still have issues, then you may
need to edit the user.js file (pref.js on mac’s), If this is the case,
please look at my previous post in this thread on how to do that.
Now it should all be working fine.
The rest of my code is designed for portability; the get function must
take a form. Since most forms will already have the server URL set in
the “action” field, we are just passing that through to the
xmlHTTPrequest. To obtain all the fields in the form, we have the
getParameters function that looks for all the different types of inputs
and adds them to the parameters string. The only problem is if the form
is in a table or has surrounding div/span elements, it won’t get those
inputs; hence we have the small code at the bottom that references
itself:
if (obj.childNodes[i].childNodes.length > 0) {
DDmap[DDlevel] = i;
DDlevel++;
setParams(obj.childNodes[i]);
DDlevel–
i = DDmap[DDlevel];
}
Since it is referencing itself, it looses track of the previous
iterator, so we need to store the interator and restore it when we come
back out of the function.
If you would prefer “GET” instead of “POST”, just change the following line:
startRequest(”xml”, “POST”, obj.action, getstr, true);
To:
startRequest(”xml”, “GET”, obj.action, getstr, true);
The code will work out the rest.
I hope this code will benefit some of you; it took me days to get it all sorted (Hence the long post).
Great Job Murali