开发者

how to call ashx file using ajax in asp.net

开发者 https://www.devze.com 2023-02-26 16:02 出处:网络
I m developing website. i want call one ashx file which return all process lis开发者_开发问答t on serverStep: 1 Create the handler and the server side code like the following

I m developing website. i want call one ashx file which return all process lis开发者_开发问答t on server


Step: 1 Create the handler and the server side code like the following

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Linq;
public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";

        var processes = System.Diagnostics.Process.GetProcesses();
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        context.Response.Write(serializer.Serialize(processes.Select(p => p.ProcessName)));


    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Step 2: Give the necessary permissions for your asp.net process / application pool. In development version you can add the impersonation tag in the web.config as a shortcut.

<identity impersonate="true" userName="windows/usernamer" password="password"/>

Step 3: Use the handler's result from the aspx page via ajax or otherwise. An example is as follows using the jquery library.

<%@ Page Title="Home Page" Language="CS" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js">

</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <ul id="processes">

    </ul>

    <script type="text/javascript">
        $(function () {
            $.get("handler.ashx", function (data) {
                $.each(data, function (index, element) {
                    $("#processes").append($("<li />").html(element));
                });
            });
        });    
    </script>
</asp:Content>

Hope this helps.

0

精彩评论

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