以下是我的JQuery代码:
$("#ddlMSP").empty().append("<option selected='selected' value='0'>Loading...</option>");
$.ajax({
type: "POST",
url: "GetMSPs.ashx",
contentType: "application/json; charset=UTF-8",
data: { UserID: 10},
dataType: "json",
success: function (response) {
if (response.length > 0) {
$("#ddlMSP").empty().append('<option selected="selected" value="0">--select--</option>');
$.each(response, function () {
$("#ddlMSP").append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
else {
$("#ddlMSP").empty().append('<option selected="selected" value="0">Not available</option>');
}
},
failure: function (response) {
alert("failure");
},
error: function (jqXHR, txtStatus, errThrown) {
alert("Error : " + txtStatus + ";" + errThrown);
}
});
以下是我的asp.net通用处理程序ProcessRequest方法:
Dim MSPs As New ArrayList
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SMT").ConnectionString)
con.Open()
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "select userid, fname + ' ' + lname AS Name from U " & _
"where userid in (select ut1.userid from ut1, ut2 " & _
"where ut1.TID = ut2.TID and ut2.UserID=@UserID)
cmd.Parameters.AddWithValue("@UserID",context.Request.Form(0))
Dim rdr As SqlDataReader = cmd.ExecuteReader
While rdr.Read
MSPs.Add(New ListItem(rdr("Name").ToString, rdr("UserID").ToString))
End While
rdr.Close()
cmd.Dispose() : cmd = Nothing
con.Dispose() : con = Nothing
context.Response.ContentType = "application/json"
Dim JSerializer As New Script.Serialization.JavaScriptSerializer
context.Response.Write(JSerializer.Serialize(MSPs))
如果我运行此代码,我会在context.Request.Form(0)上得到错误。它说索引超出范围,因为Request.Form中没有任何内容 如果我将JQuery ajax与GET一起使用而不是Request.Form,则使用Request.QueryString,此代码有效。
所以我的问题是,在使用带有POST的jquery ajax时我做错了什么?我已经阅读了很多关于jquery ajax的问题,但他们的方法都没有对我有用。
有人可以分享他/她的专业知识吗?