我试图调用我的自定义Htpp处理程序,并希望传递一些参数,但我无法在http处理程序进程请求方法上检索这些参数的值.我用的代码就像..
在客户端
$.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);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我自定义的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"];
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我问题出在哪里.
Darm 的回答是正确的,但只是为了更清楚地了解为什么会这样......
您需要删除该行,contentType: 'application/json;charset=utf-8'以便 $.ajax() 使用默认值contentType: 'application/x-www-form-urlencoded; charset=UTF-8'(如$.ajax 文档中指定的)。通过单独执行此操作,您的代码示例应该可以工作(dataType 参数实际上指定了您希望从服务器返回的数据类型)。
以最简单的形式,您可以像这样编写 $.ajax() 调用:
$.ajax({
url: 'VideoViewValidation.ashx',
data: { 'Id': '10000', 'Type': 'Employee' },
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,请求将通过 GET 发出,参数将通过查询字符串发送,但它仍然适用于context.Request.