Friday, July 20, 2012

How to call .ashx file through jquery

your aspx page.
add the latest jquery into the head section

<script src="" type="text/javascript"></script>

//document ready
            type: "POST",
            url: "handler.ashx",
            data: { firstName: 'Rahul', lastName: 'Kumar' },
            // DO NOT SET CONTENT TYPE to json
            // contentType: "application/json; charset=utf-8",
            // DataType needs to stay, otherwise the response object
            // will be treated as a single string
            dataType: "json",
            success: function (response) {

your handler.ashx file

    using System;
    using System.Web;
    using Newtonsoft.Json;

    public class Handler : IHttpHandler
        public void ProcessRequest(HttpContext context)
            context.Response.ContentType = "text/plain";

            string myName = context.Request.Form["firstName"];

            // simulate Microsoft XSS protection
            var wrapper = new { d = myName };
            // in order to use JsonConvert you have to download the
            // Newtonsoft.Json dll from here

        public bool IsReusable
                return false;

1 comment:

  1. can you explain how pass the value from jquery to ashx file???