我正在尝试调用网络函数,但我不知道我是否犯了任何错误。 这是以下功能:
[WebMethod]
public string NumberOfHazardsToUser(string userid)
{
JavaScriptSerializer json = new JavaScriptSerializer();
command.CommandText = "select count(*) from Users_Hazards where User_ID = '" + userid + "'";
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
if ((int)reader.GetValue(0) != 0)
{
int count = (int)reader.GetValue(0);
command.Connection.Close();
return json.Serialize(count);
}
}
command.Connection.Close();
return json.Serialize(0);
}
我是这样调用它的
function getCountHazardsToUser() {
$.ajax({
async: true,
url: web + "/NumberOfHazardsToUser",
method: "GET",
data: '{"userid":"' + localStorage.getItem("id") + '"}',
dataType: "json",
contentType: "application/json",
success: function (data) {
var res = data.d;
hazardsNumber = res;
},
error: function () {
hazardsNumber = -1;
}
});
这是回复 System.InvalidOperationException:缺少参数:userid。
最佳答案
我已经对您的代码进行了一次修改(更改 method:POST
)并运行良好。
$.ajax({
async: true,
url: web + "/NumberOfHazardsToUser",
method: "POST",
data: '{"userid":"' + localStorage.getItem("id") + '"}',
dataType: "json",
contentType: "application/json",
success: function (data) {
var res = data.d;
hazardsNumber = res;
},
error: function () {
hazardsNumber = -1;
}
});
如果您需要任何帮助,请告诉我
关于javascript - WebService C# (Ajax) 中缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047946/