4

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

3 Answers 3

1

If you are not using .NET 4 then you need to do configuration entries to enable script service. See http://msdn.microsoft.com/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());
}
1

Try applying [ScriptMethod] attribute on the PieTable method, this usually solves my problems when calling webmethods with JQuery.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.