3

I am trying to call my custom Htpp Handler and want to pass some parameter's but i am unable to retrieve those param's value on the http Handler process request method. I use code like ..

At Client Side

$.ajax({
                url: 'VideoViewValidation.ashx',
                type: 'POST',
                data: { 'Id': '10000', 'Type': 'Employee' },
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    debugger;
                    alert('Server Method is called successfully.' + data.d);
                },
                error: function (errorText) {
                    debugger;
                    alert('Server Method is not called due to ' + errorText);
                }
            });

And this is on my custom http Handler

public class VideoViewValidation : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string videoID = string.Empty;
        string id = context.Request["Id"];
        string type = context.Request["Type"];
}
}

Please tell me where is the problem .

Abhishek
  • 957
  • 3
  • 20
  • 43

4 Answers4

6

Remove "contentType: 'application/json;charset=utf-8'" and add "dataType:'json'"

Darm
  • 5,581
  • 2
  • 20
  • 18
  • can you tell me its not working when i was using contentType instead of dataType ? – Abhishek Jul 26 '11 at 09:52
  • Remove "debugger;". Use "console" and see if the request is sent and what is the content of the response. – Darm Jul 26 '11 at 09:56
5

Darm's answer was correct, but just to be clearer about why this works...

You need to remove the line contentType: 'application/json;charset=utf-8' so that $.ajax() uses the default of contentType: 'application/x-www-form-urlencoded; charset=UTF-8' (as specified in the docs for $.ajax). By doing this alone, your code sample should work (the dataType parameter actually specifies the data type that you're expecting to be returned from the server).

In its simplest form, you could write the $.ajax() call like this:

$.ajax({
    url: 'VideoViewValidation.ashx',
    data: { 'Id': '10000', 'Type': 'Employee' },
});

In this case, the request would be made via GET and the parameters would be sent via the query string, but it still works with context.Request.

Derek Morrison
  • 5,456
  • 4
  • 31
  • 24
0

Your data is sent to the handler using an ajax call (not from a typical form submit). To retrieve the data you just need context.Request["Id"]. that should do the trick.

0

Your jquery is sending POST data so you will need to look at the Request.Form["Id"] etc

public class VideoViewValidation : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string videoID = string.Empty;
        string id = context.Request.Form["Id"];
        string type = context.Request.Form["Type"];
}
}
Richard
  • 21,728
  • 13
  • 62
  • 101
  • I tried it but its not working. its resulting null value. please try yourself. – Abhishek Jul 26 '11 at 09:47
  • This is covered by Abisheks's original code. The context.Request member has a default indexer that "Gets the specified object from the System.Web.HttpRequest.QueryString, System.Web.HttpRequest.Form, System.Web.HttpRequest.Cookies, or System.Web.HttpRequest.ServerVariables collections.". – Derek Morrison Sep 19 '12 at 13:29