Javascript Web 服务方法名称无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11878064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:30:43  来源:igfitidea点击:

Web Service method name is not valid

javascriptasp.netjquery

提问by ravidev

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript

当我尝试从 javascript 调用 webmethod 时,出现以下错误“Web 服务方法名称无效”

System.InvalidOperationException: SaveBOAT Web Service method name is not valid. at System.Web.Services.Protocols.HttpServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

System.InvalidOperationException:SaveBOAT Web 服务方法名称无效。在 System.Web.Services.Protocols.HttpServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型,HttpContext 上下文,HttpRequest 请求,HttpResponse 响应)在 System.Web.Services.Protocols.ServerProtocolFactory。创建(类型类型、HttpContext 上下文、HttpRequest 请求、HttpResponse 响应、Boolean&abortProcessing)

HTML Code :

HTML代码:

<asp:LinkButton runat="server" ID="lnkAddBoat" OnClientClick="javascript:AddMyBoat(); return false;"></asp:LinkButton>

JS Code :

JS代码:

function AddMyBoat() {
            var b = document.getElementById('HdnControlId').value;

            jQuery.ajax({
                type: "GET",
                url: "/AllService.asmx/SaveBOAT",
                data: { Pid: b },
                contentType: "application/text",
                dataType: "text",
                success: function(dd) {
                    alert('Success' + dd);
                },
                error: function(dd) {
                    alert('There is error' + dd.responseText);
                }
            });
}

C# Code (Web method in AllService.asmx file)

C# 代码(AllService.asmx 文件中的 Web 方法)

[WebMethod]
public static string SaveBOAT(int Pid)
{
    // My Code is here
    //I can put anythng here
    SessionManager.MemberID = Pid;
    return "";
}

I tried all solutions found on Stack Overflow and ASP.NET site.but none of them worked for me.

我尝试了在 Stack Overflow 和 ASP.NET 站点上找到的所有解决方案。但没有一个对我有用。

回答by ravidev

It was a silly mistake.

这是一个愚蠢的错误。

remove Statickeyword from method declaration.

Static从方法声明中删除关键字。

[WebMethod]
public string SaveBOAT(string Pid)
{        
     SessionManager.MemberID = Pid;
     return "";
}

回答by Alessio

In my case the error was that the Web Service method was declared "private" instead of "public"

在我的情况下,错误是 Web 服务方法被声明为“私有”而不是“公共”

回答by Matthew Lock

In my case I had copied another asmx file, but not changed the class property to the name of the new class in the asmx file itself (Right click on asmx file -> View Markup)

在我的情况下,我复制了另一个 asmx 文件,但没有将类属性更改为 asmx 文件本身中新类的名称(右键单击 asmx 文件 -> 查看标记)

回答by Rohit

Try using this, I think datatype should be JSON

尝试使用这个,我认为数据类型应该是 JSON

       jQuery.ajax({
            type: "POST",  // or GET
            url: "/AllService.asmx/SaveBOAT",
            data: { Pid: b },
            contentType: "application/json; charset=utf-8",
            dataType: "json"
            success: function(dd) {
                alert('Success' + dd);
            },
            error: function(dd) {
                alert('There is error' + dd.responseText);
            }
        });

And in C# Code change Pid to string

并在 C# 代码中将 Pid 更改为字符串

    [WebMethod]
     public static string SaveBOAT(string Pid)
     {        
      SessionManager.MemberID = Pid;
      return "";
     }

回答by Sundar Rajan

I too faced the similar issue. The solution includes checking everything related to ensuring all name, parameters are passed correctly as many have responded. Make sure that the web method name that we are calling in UI page is spelled correctly, the data, data types are correct and etc. In my case, I misspelled the web method name in my ajax call. It works fine once I found and corrected the name correctly.
For Ex: In .asmx class file, this is the method name "IsLeaseMentorExistWithTheSameName" but when I called from UI this is how I called:

我也遇到了类似的问题。该解决方案包括检查与确保所有名称、参数都正确传递的所有相关内容,因为许多人已做出响应。确保我们在 UI 页面中调用的 Web 方法名称拼写正确,数据、数据类型正确等。就我而言,我在 ajax 调用中拼错了 Web 方法名称。一旦我找到并正确更正了名称,它就可以正常工作。
例如:在 .asmx 类文件中,这是方法名称“IsLeaseMentorExistWithTheSameName”但是当我从 UI 调用时,我是这样调用的:

var varURL = <%=Page.ResolveUrl("~/Main/BuildCriteria.asmx") %> + '/IsLeaseMentorExistWithSameName';  

Notice that the word "The" is missing. That was a mistake and I corrected and so it worked fine.

请注意,缺少“The”一词。那是一个错误,我纠正了,所以效果很好。

回答by Nag

Did U add ServiceReferenceClass. Check thisonce. Based on your comment I can tell what to do

你有没有添加ServiceReference类。检查一次。根据您的评论,我可以告诉您该怎么做

回答by Antoni

I had this issue because my soap method had a List<string>parameter. Couldn't figure out a way to make it work with the array parameter; so just converted the parameter to a &-delimited string (e.g. val1&val2&val3) and converted the parameter to an array in the service method.

我有这个问题,因为我的肥皂方法有一个List<string>参数。无法想出一种方法使其与数组参数一起工作;所以只需将参数转换为带&分隔符的字符串(例如val1&val2&val3)并将参数转换为服务方法中的数组。

回答by Wilco

As Sundar Rajan states, check the parameters are also correct. My instance of this error was because I had failed to pass any parameters (as a body in a POST request) and the asmx web method was expecting a named parameter, because of this the binding logic failed to match up the request to the method name, even though the name itself is actually correct.

正如 Sundar Rajan 所说,检查参数是否正确。我的此错误实例是因为我未能传递任何参数(作为 POST 请求中的主体)并且 asmx Web 方法需要一个命名参数,因此绑定逻辑无法将请求与方法名称匹配,即使名称本身实际上是正确的。

[WebMethod]
        public object MyWebMethod(object parameter)

If there is no parameterin the body of the request then you will get this error.

如果parameter请求正文中没有,那么您将收到此错误。