Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameter to Http Handler from jQuery call

Tags:

jquery

asp.net

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 .

like image 422
Abhishek Avatar asked Apr 09 '26 04:04

Abhishek


2 Answers

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

like image 196
Darm Avatar answered Apr 11 '26 19:04

Darm


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.

like image 35
Derek Morrison Avatar answered Apr 11 '26 18:04

Derek Morrison