【问题标题】:Missing parameter in WebService C# (Ajax)WebService C# (Ajax) 中缺少参数
【发布时间】:2018-06-11 09:22:12
【问题描述】:

我正在尝试调用一个网络函数,但我不知道我是否犯了任何错误。 这是函数:

[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:缺少参数:用户 ID。

【问题讨论】:

    标签: javascript c# ajax web-services


    【解决方案1】:

    我已经对您的代码进行了一次修改(更改 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;
        }
    });
    

    如果您需要任何帮助,请告诉我

    【讨论】:

    • 他的方法不是帖子
    • @Sajeetharan 发布后我能够接收数据
    • 你没事,我改成POST了,不知道为什么不能get。
    • @KaramHaj :您正在将数据发布到服务器
    • 我得到了 {"d":"5"},很好,全局值没有更改为 5 它仍然是 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2019-04-04
    • 2017-02-24
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多