ASP.NET: Using XMLHTTP With ASP.NET and JavaScript, Server Processing Without PostBacks

Hi, Last week I was working on a web interface, Where I wanted to ge

ITPro Today

January 9, 2006

2 Min Read
ITPro Today logo in a gray background | ITPro Today

Hi,

 

Last week I was working on a web interface, Where I wanted to get/post some data from/to the server in XML Format without posting back the page.

Well, XMLHTTP was the best method around with POST method.

Here is the complete sample for using XMLHTTP Post Calls Using JavaScript And ASPX Pages.

 

Basically, The method can be implemented in the following manner:

 

For Client:

  1. Create a HTML Page, add the given JavaScript To It.

  2. Add the given HTML Button on the page.

For Server:

  1. Create an ASPX Page.

  2. Remove the HTML, Head and Body Sections, so that it contains only the Code Behind linking information.

  3. In The Page Load Event of this page, Create a StreamReader Object.

  4. Read The Input Stream In The Reader, Load it in XML Document Object.

  5. Process The XML as required.

  6. Send back the XML to the client calling.

Some important points are:

  • Before sending the XML as String to the ASPX Page, replace the < with < and > with > to avoid a Potentially Dangerous Request Error. (The given function is already doing that)

 

So The basic flow is like:

  • Do the required actions to get the XML Data.

  • Post the XML Data as string to the aspx page.

  • Read the Posted XML Data in the ASPX Page through Input Stream.

  • Do the modifications required in the XML Data after loading it in a XML Document object.

  • Write the XML Data as String back in the Response.

  • Read the data sent back by the ASPX Page by using responseText property in JavaScript.

 

Regards

Shobhit

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like