Javascript - ASP.NET WebService call - Failed to start server methodName method

I tried to solve this problem for a while and have not had time. I have a basic ASP.NET WebService that I am 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 ... huh? Then why, when I try to call it from javascript, I get the following:

"Error: The PieTable server method failed to execute.

I call 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 javascript from the following object:

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

I use the AJAX ToolkitScriptManager object to reference the WebService:

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

Help. Thank you in advance. Marco

+4
source share
3 answers

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

To troubleshoot, you can see the exception stack trace - for example,

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

Try using the [ScriptMethod] attribute in the PieTable method, this usually solves my problems when invoking web methods using 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"; } 
+1
source

In the past, I found that put () after the ScriptService in the attribute declared in the web service class seems to have solved some strange problems. I don’t know why this could work, but it’s worth a try in your case.

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

All Articles