【问题标题】:Unknown web-method, trying to send json using AJAX未知的网络方法,尝试使用 AJAX 发送 json
【发布时间】:2016-10-10 07:36:24
【问题描述】:

我正在尝试将一些数据(参数)从客户端(html)传递到服务器端(C# 代码隐藏)到一个方法,这是使用 JSON 格式的 AJAX 完成的,但我得到以下错误:

未知的网络方法

我的 AJAX 代码是:

var jsonObj = { "sCriterion": sCriterion };
            $.ajax({
                type: "POST",
                url: "NewToken.aspx/GetSelection",
                data: jsonObj,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Request: " + JSON.stringify(XMLHttpRequest) + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
                },
                success: function (result) {
                    alert(data);
                    alert("We returned: " + result);
                }
            });

这是我的代码隐藏方法:

[WebMethod]
private static string GetSelection(string selectedItem)
{

    var json = new JavaScriptSerializer();
    var data = json.Deserialize<Dictionary<string, Dictionary<string, string>>[]>(selectedItem.ToString());
    var jsonObj = json.Serialize("proceeded");
    return jsonObj;
}

【问题讨论】:

    标签: c# json ajax


    【解决方案1】:

    该方法应该是public static 才能工作。不是私人的!

    [WebMethod]
    public static string GetSelection(string selectedItem)
    {
    
        var json = new JavaScriptSerializer();
        var data = json.Deserialize<Dictionary<string, Dictionary<string, string>>[]>(selectedItem.ToString());
        var jsonObj = json.Serialize("proceeded");
        return jsonObj;
    }
    

    【讨论】:

      【解决方案2】:

      您的GetSelection 方法必须是public,但您将其设置为私有。

      【讨论】:

        猜你喜欢
        • 2012-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        相关资源
        最近更新 更多