简体   繁体   English

Javascript-调用ASP.NET WebService-服务器方法'methodName'失败

[英]Javascript - calling ASP.NET WebService - The server method 'methodName' failed

I've been trying to resolve this issue for a while and haven't been successful yet. 我已经尝试解决此问题一段时间了,但尚未成功。 I've got a basic ASP.NET WebService which I'm trying to call from javascript as such. 我有一个基本的ASP.NET WebService,我正尝试从javascript这样调用。

using System;
using System.Web;
using System.Web.Services.Protocols;
using System.Web.Services;
using System.Web.Script.Services;

namespace RandomWebServices
{
/// <summary>
/// Summary description for WebServices1
/// </summary>
[WebService(Namespace = "http://localhost:2900/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebServices1 : WebService
{
    [WebMethod]
    public string PieTable(string table)
    {
        return table + " - resultant text";
    }
}
}

Simple... Yes? 简单...是吗? Then why is it that when I try calling it from javascript I get the following: 那么为什么当我尝试从javascript调用它时却得到以下信息:

"Error: The server method 'PieTable' failed." “错误:服务器方法'PieTable'失败。”

I call the WebService as follows: 我将WebService称为如下:

<script type="text/javascript">
function CallService() {
    RandomWebServices.WebServices1.set_defaultSucceededCallback(Callback);
    RandomWebServices.WebServices1.set_defaultFailedCallback(OnError);

    RandomWebServices.WebServices1.PieTable("Pie");

    return false;
}

function Callback(result) {
    alert("asd");

    var outDiv = document.getElementById("outputDiv");

    if (outDiv == null) {
        alert("outputDiv not found");

        return false;
    }
    else {
        alert("outputDiv found");

        outDiv.innerText = result;
    }

    return false;
}

function OnError(result) {
    alert("Error: " + result.get_message());
}
</script>

I am calling the javascript from the following object: 我正在从以下对象调用javascript:

<input value="Load" onclick="CallService(); return false;" type="button" />

I use AJAX's ToolkitScriptManager object to reference the WebService: 我使用AJAX的ToolkitScriptManager对象来引用WebService:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
<Services>
    <asp:ServiceReference Path="http://localhost:2900/WebServices1.asmx" />
</Services>
</asp:ToolkitScriptManager>

Please assist. 请协助。 Thanks in advance. 提前致谢。 Marco 马可

If you are not using .NET 4 then you need to do configuration entries to enable script service. 如果使用.NET 4,则需要进行配置条目以启用脚本服务。 See http://msdn.microsoft.com/en-us/library/bb398998(v=VS.90).aspx . 请参阅http://msdn.microsoft.com/zh-cn/library/bb398998(v=VS.90).aspx So make sure that you have following section in web.config. 因此,请确保在web.config中有以下部分。

<system.web>
  ...
  <httpHandlers>
    <remove verb="*" path="*.asmx"/> 
    <add verb="*" path="*.asmx" 
      type="System.Web.Script.Services.ScriptHandlerFactory"
       validate="false"/>
  </httpHandlers>
  ...
<system.web>

For trouble-shooting, you can look at the stack trace of the exception - for example, 要进行问题排查,您可以查看异常的堆栈跟踪-例如,

function OnError(result) {
    alert("Error: " + result.get_message());
    alert("Stack Trace: " + result.get_stackTrace());
}

Try applying [ScriptMethod] attribute on the PieTable method, this usually solves my problems when calling webmethods with JQuery. 尝试在PieTable方法上应用[ScriptMethod]属性,当使用JQuery调用Web方法时,这通常可以解决我的问题。

http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx http://msdn.microsoft.com/zh-CN/library/system.web.script.services.scriptmethodattribute.aspx

[WebMethod]
[ScriptMethod]
public string PieTable(string table)
{   
    return table + " - resultant text"; 
} 

I have found in the past that putting () after ScriptService in the attribute declared on the web service class has seemingly solved certain bizarre problems. 我过去发现,在Web服务类上声明的属性中,将()放在ScriptService之后似乎解决了一些奇怪的问题。 No idea why it might have worked but worth a try in your case too. 不知道为什么它可能会起作用,但也值得您尝试一下。

ie

[WebService(Namespace = "http://localhost:2900/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class WebServices1 : WebService
{
    [WebMethod]
    public string PieTable(string table)
    {
        return table + " - resultant text";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM