passing parameter to Http Handler from jQuery call

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 .

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Remove “contentType: ‘application/json;charset=utf-8′” and add “dataType:’json’”

Method 2

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.

Method 3

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"];
}
}

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x