For the last few days ago, I was working with HTTP handler. Here I was required to call handler from Client script. Later I found that I needed to pass some data to handler and also receive some from it.

So I was searching if there is some way we can pass some data to handler in some format and can receive too.

Here I am going to share how we can call a HTTPHandler from jQuery and pass some data to handler and receive as well.

Here url is the url of your Handler. contentType defines the content of the request. On successful completion of the request, the OnComplete method will be called. If there is any error of the request, the OnFail will be called. The dummy methods are:

If you want to receive data from Handler at Client side, how will you move ahead? It’s very easy to return data in JSON format.Here in my sample example, I have created one Class Employee and return it from handler after serializing with the help of JavaScriptSerializer. My class is as follows:

Now you can receive the data and use it as per your requirement on the client side. Above here, I have just shown it as alert popup.

I think this post will help you a lot as this can be used in different scenarios. The classic example could be.“In many situation, we get the task to upload files in our applications with lots of validations; it could be validations like file type, content size and some business validations as well. All these validations could not be handled on client side only. One approach could be HTTPHandler which we can use for seamless validations.”

Share

About the Author

Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Is the backslash a typo? Also do the datatypes of the handler not have to match the call to the handler? The handler is using context.Response.ContentType = "text/html" but jquery uses:application/jsonI have copied your code fairly closely. When I call the handler from the address bar I get:{Id:MyID, Name:Test, myInt:999} (each inside speechmarks)But in the jquery the OnComplete function is called but does not return results:function OnComplete(result) { alert([result.Id, result.Name]); //this failsalert(result); this returns null};

Ran Fiddler and when calling the handler in the address bar fiddler shows GET followed by the url and parameters, and and the output.But when called from jquery Fiddler shows OPTIONS followed by the same url and parameters, and with no output.