未知的web方法参数方法名称

本文关键字:方法 参数 未知 web | 更新日期: 2023-09-27 18:05:16

我正在构建一个Web应用程序,其中我试图在WebForm中调用WebMethod,我已经尝试了谷歌中的每一个页面,但我仍然一无所获。这是一个Jquery Ajax调用的例子

$.ajax({
            type: "Post",
            url: "Default.aspx/Return",
            data: {dato:'Hello'},
            contentType: "application/json; chartset:utf-8",
            dataType: "json",
            success:
                    function (result) {
                        if (result.d) {
                            alert(result.d);
                        }
                    },
            error:
                function (XmlHttpError, error, description) {
                    $("#grdEmpleados").html(XmlHttpError.responseText);
                },
            async: true
        });

这是代码后面的WebMethod

[WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static string Return(string dato)
    {
        return dato;
    }

未知的web方法参数方法名称

您不能以这种方式访问Static方法。删除"Static"引用,它将工作。另外,就像其他人说的那样——不要用它作为你的方法名"Return"。

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Return(string dato)
{
    return dato;
}

我认为,在您的成功事件中使用了带有结果的函数,这是一个字符串,您正在尝试访问名为d的属性,假设结果是一个对象。仅使用alert(result);使用F12工具调试并查找错误

确保您已经在ScriptManager元素中启用了页面方法:

<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" />

和你的方法

$.ajax({
            type: "Post",
            url: '<%= ResolveUrl("~/Default.aspx/Return") %>',
            data: {dato:'Hello'},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success:
                    function (result) {
                            alert(result);
                    },
            error:
                function (XmlHttpError, error, description) {
                    $("#grdEmpleados").html(XmlHttpError.responseText);
                },
            async: true
        });

试试这个

var url = window.location.pathname + "/Return";
$.ajax({
        type: "Post",
        url: url,
        data: {dato:'Hello'},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:
                function (result) {
                        alert(result.d);
                },
        error:
            function (XmlHttpError, error, description) {
                $("#grdEmpleados").html(XmlHttpError.responseText);
            },
        async: true
    });`