.net c#未知的Web方法参数方法名称

Unknown web method parameter method name
2021-09-14
  •  译文(汉语)
  •  原文(英语)

我正在构建一个Web应用程序,在其中尝试在WebForm中调用WebMethod,我已经尝试了google中的每个页面,但仍然一无所获.这是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;
    }
速聊1:
我得到了未知的WebMethod参数MethodName
速聊2:
对于初学者,我绝不会创建与我使用的语言共享相同保留字的Return方法,我认为这不是一个好的方法名
速聊3:
这只是示例……我实际上并未使用此方法,但是无论如何,我正在编写的代码结构是相同的
速聊4:
什么版本的.Net?这是网站应用程序还是网站?
速聊5:
我认为Framework 4.0是一个网站,我该如何注意区别?
解决过程1

您不能以这种方式访问​​静态方法.删除"静态"引用,它将起作用.另外,就像其他人所说的-请勿将其用作您的方法名称"Return".

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Return(string dato)
{
    return dato;
}
解决过程2

我认为,在您的成功事件上,将使用带有结果的函数,该函数为字符串,并且您假设结果为对象,您正在尝试访问名为d的属性.仅alert(result); 使用User F12工具调试并查找错误.

速聊1:
我将webmethod响应配置为json
速聊2:
application/json; charset=utf-8不是图表集
速聊3:
我只是尝试了您所说的内容,但仍然无法正常工作...我现在讨厌Asp
速聊4:
尝试在您的Web方法的function和scriptmethod属性中删除static关键字.并将"chartset"重命名为"charset".
解决过程3

确保在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
        });
解决过程4

试试这个

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
    });`
速聊1:
这也没有用:/ ...但是我实际上认为在代码背后有一些东西...就像服务器没有注册网络方法之类的东西一样..........................................

I'm building a Web Application in which I'm trying to call a WebMethod in a WebForm, I've tried every single page in google but I'm still getting nothing. This is an example of the Jquery Ajax Call

$.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
        });

And this is the WebMethod in the codebehind

[WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static string Return(string dato)
    {
        return dato;
    }
Talk1:
I got the Unknown WebMethod Parameter MethodName
Talk2:
For starters I would never create a method that shares the same reserved word as in the language that I am coding in Return not a good Method name in my opinion
Talk3:
this is just the example... I'aint using this method actually, but no matter what, the code structure tha I'm writing is the same
Talk4:
What version of .Net? Is this a web application or web site?
Talk5:
Framework 4.0 and I think is a website, how should I note the difference?
Solutions1

You can't access a Static method this way. Remove the "Static" reference and it will work. Also, like someone else said - do not use that as your method name "Return".

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

I think, on your success event the function with result is used, which is a string and u are trying to access property named d assuming result is an object. use of only alert(result); User F12 tool to debug and find your error.

Talk1:
I configured the webmethod response to json
Talk2:
it's application/json; charset=utf-8 not chartset
Talk3:
I just tried what you said, but still not working... I'm hating Asp right now
Talk4:
try removing static keyword in your function and scriptmethod attribute for your web method. And also rename 'chartset' to 'charset'.
Solutions3

Make sure that you have enabled page methods in your ScriptManager element:

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

and your method

$.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
        });
Solutions4

Try this

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
    });`
Talk1:
This didn't work too :/ ... but I actually think that there's something in the codebehind ... it's like the server doesn't register the webmethod or something
转载于:https://stackoverflow.com/questions/15576696/unknown-web-method-parameter-method-name

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入