开发者

Use AJAX with aspx backend?

开发者 https://www.devze.com 2023-02-22 13:11 出处:网络
Very new to AJAX, but have learned a bit from online tutorials.If I was using PHP all would be well, but...

Very new to AJAX, but have learned a bit from online tutorials. If I was using PHP all would be well, but...

...I have to use VB.Net. Microsoft provides black magic tools (in the Visual Studio IDE) for making AJAX work, but using these seems hopelessly complicated and documentation for these tools covers only the simplest examples. By contrast, writing the scripts myself seems much e开发者_高级运维asier, and I will understand what is going on.

So my question is, is it possible to write my own AJAX javascript (creating the XMLHttpRequest object, etc.), and have the server-side function be written in VB.Net instead of PHP? Has anyone ever tried this?


Of course. You can implement an HttpHandler on the server to give you full control of what happens with the request and the response. The Microsoft AJAX thing is kind of like a black magic box, but ASP.NET is not.

Here is a starting point:

public class MyHandler : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("Hello AJAX World");
    }
}

And don't forget to register the handler in the web.config ;)

<add name="ajax_page.ashx*" 
     path="ajax_page.ashx" 
     verb="*" 
     type="MyNamespace.MyHandler, MyAssembly" />

Here is my best guess at what this should look like in VB.NET (NOTE: I have never written a single line of VB.NET)

Public class MyHandler 
    Inherits IHttpHandler

    Public Property IsReusable() As Boolean
        Get
            Return True
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context as HttpContext)
        context.Response.Write("Hello AJAX World")
    End Sub
End Class
0

精彩评论

暂无评论...
验证码 换一张
取 消