c# - Javascript - calling ASP.NET WebService - The server method 'methodName' failed - Stack Overflow

I've been trying to resolve this issue for a while and haven't been successful yet.I've

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.

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:

"Error: The server method 'PieTable' failed."

I call the WebService as follows:

<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:

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

I use AJAX's ToolkitScriptManager object to reference the 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

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.

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:

"Error: The server method 'PieTable' failed."

I call the WebService as follows:

<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:

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

I use AJAX's ToolkitScriptManager object to reference the 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

Share Improve this question edited Jul 6, 2015 at 8:24 Dyrandz Famador 4,5255 gold badges27 silver badges40 bronze badges asked Jun 22, 2011 at 10:13 Marco CannonMarco Cannon 411 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

If you are not using .NET 4 then you need to do configuration entries to enable script service. See http://msdn.microsoft./en-us/library/bb398998(v=VS.90).aspx. So make sure that you have following section in 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.

http://msdn.microsoft./en-us/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. No idea why it might have worked but worth a try in your case too.

i.e.

[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";
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745095059a4610925.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信