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)开发者_Python百科
{
string videoID = string.Empty;
string id = context.Request["Id"];
string type = context.Request["Type"];
}
}
Please tell me where is the problem .
Remove "contentType: 'application/json;charset=utf-8'" and add "dataType:'json'"
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
.
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"];
}
}
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.
精彩评论