Ajax Help
I am using asp.net 2003 and am using ajax in my application.currently
what am doing is that in my project i have another aspx page named ajaxEngine where all the server processes are done.After that i call a response.write() to send the xml file to my client side aspx page.
My doubt is that can't i use the code behind page of the same client side aspx page instead of another page like ajaxEngine
# 4 Re: Ajax Help
Binil,
I just worked on something like this, too. I have .aspx pages and .aspx.vb code behinds. I am sending all my ajax requests via POST to AjaxHandler.vb. Our methods sound very similar.
First, I had to change all the urls that I send the request to. Instead of sending to myPage.aspx, I am sending to AjaxHandler.ashx. The request is forwarded to the AjaxHandler class by a modification in the web.config. Then, AjaxHandler does its work and sends back some xml to the client.
This is how I open the request in javascript:
this._xmlhttp.open('post', url, true);
And I send my xml document like this ( strUrl = AjaxHandler.ashx ):
a.call(strUrl, xmlDoc);
This is what I had to put into web.config (It goes in <system.web>):
<httpHandlers>
<!-- Simple Handler -->
<add verb="*" path="Ajax.ashx" type="GLRecon.Ajax, GLRecon" />
</httpHandlers>
Then, the AjaxHandler class looks a lot like this. It has to implement IHttpHandler...
Imports System.Web
Imports System.Xml
Public Class Ajax
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim xmlDoc As XmlDocument
'If the XML document was submitted via post method, then load it in the DOM document
If (context.Request.InputStream.Length > 0 And context.Request.ContentType.ToString().IndexOf("/xml") > 0) Then
xmlDoc = New XmlDocument
xmlDoc.Load(context.Request.InputStream)
Select Case xmlDoc.GetElementsByTagName("Command").Item(0).InnerText
Case "Bar"
Call Bar()
Case "Func2"
Call Func2()
Case "HelloWorld"
Call HelloWorld()
...etc...
End Select
End If
End Sub
Public Sub Bar()
'Get params, do some work, and return something in the Response...
End Sub
End Class
Public Sub ReturnXML(ByVal context As HttpContext, ByVal rawXml As String)
If (rawXml = "") Then
Throw New ApplicationException("The value of rawXml was not defined.")
End If
context.Response.ContentType = "application/xml"
Dim xw As New XmlTextWriter(context.Response.OutputStream, New System.Text.UTF8Encoding)
xw.WriteRaw(rawXml)
xw.Close()
End Sub
End Class
Hope this helps you, my code is not fully functional as I could not post that to the forum, but this is pretty close, and should give you a good starting point, even though it's vb.net instead of c#.
-Ranthalion