Sending XML to the Sever with AJAX
A question that has come up to lately by people on JavaRanch and emails I received is how do I send an XML to the server using Ajax. It is possible to send an XML document in the request parameter. One thing you need to make sure is that you set the header correctly to text/xml and post the document to the server. This following is a basic example that highlights the fact that we are not limited to just sending query string and form values to the server, which seems to be implanted into certain developer’s minds.
For this example code I am taking two user inputs and creating an XML string. The string is sent via Ajax to the server, which reads the input. The server takes this XML information and creates a new XML document. This XML document is saved in a folder on the server. A confirmation id (GUID) is sent back to the user as shown in the following figure below.
And below is the code that you can play with.
The ClientSide Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>testAj</title>
<script type="text/javascript">
var reqXML;
function LoadXMLDoc(url){
if (window.XMLHttpRequest){
reqXML = new XMLHttpRequest();
}
else if(window.ActiveXObject){
reqXML = new ActiveXObject("Microsoft.XMLHTTP");
}
if(reqXML){
document.getElementById("spanOut").innerHTML = "Sending Information...";
reqXML.open("POST", url, true);
reqXML.onreadystatechange = BuildXMLResults;
reqXML.setRequestHeader("Content-Type", "text/xml");
var strName = encodeURI(document.Form1.txtName.value);
var strMess = encodeURI(document.Form1.txtMess.value);
var xmlBody = "<entry>";
xmlBody += "<name>" + strName + "</name>";
xmlBody += "<message>" + strMess + "</message>";
xmlBody += "</entry>";
reqXML.send("<?xml version='1.0' encoding='UTF-8'?>" + xmlBody);
}
else{
alert("Your browser does not support Ajax");
}
}
function BuildXMLResults(){
if(reqXML.readyState == 4){
if(reqXML.status == 200){
var strText = reqXML.responseText;
document.getElementById("spanOut").innerHTML = strText;
}
else{
alert("There was a problem retrieving the XML data:\n" + reqXML.statusText);
}
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server" onsubmit="LoadXMLDoc('GetXML.aspx'); return false;">
<span style="width:70px">Name: </span><input type="text"
name="txtName"><br>
<span style="width:70px">Message: </span><textarea
name="txtMess"></textarea><br>
<input type="submit" name="btnSubmit" value="Submit"><br/>
<span id="spanOut"></span>
</form>
</body>
</HTML>
The Server Side Code:
GetXML.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="GetXML.aspx.vb" Inherits="datagrid.GetXML"%>
GetXML.aspx.vb
Imports System.IO
Imports System.Xml
Imports System.Text
Public Class GetXML
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Hope this gets you started on something interesting!
Eric Pascarello
Moderator of HTML/JavaScript at
www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint
for Dynamic Web Pages
Co-Author of: Ajax in Action