Search code examples
jqueryasp.netajaxweb-serviceswebmethod

Call to WebMethod from jQuery ajax gives Internal Sever error and Web Service method is not valid


I am trying to call the the GetPlatformNames() webmethod using ajax inside the GetPlaform() javascript function.
But, the ajax calls never succeeds.
In the error alert I get "Internal Server Error" message.
I used firebug and placed a breakpoint on the alert in the error. The request.responstext states the following:

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

This is the code that I am using.
I am using Visual Studio 2012.
I am using the built-in IIS.

    namespace XYZ
    {
//// <summary>
/// Summary description for Platform
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Platform : System.Web.Services.WebService
{
    [WebMethod]        
    public static string GetPlatformNames()
    {
        var result ="Hello World";
        return result;
    }
    [WebMethod]
    public static string GetServerTimeString()
    {
        return "Current Server Time: " + DateTime.Now.ToString();
    }
    [WebMethod]
    public static void AddaPlatform(string platforName)
    {
        //Code to insert into database
    }
}   
  }

function GetPlatforms() {  
var param = '{platformName:"'+$("#platformNameInputTextBoxId").val()+'"}';
$.ajax({
    type: 'POST',
    url: 'Platform.asmx/GetPlatformNames',
    statusCode: {
        404: function () {
            alert('Could not contact server.');
        },
        500: function () {
            alert('A server-side error has occurred.');
        }
    },
    success: function (data) {
        alert(data);
    },
    error: function(request,status,error) {
        alert(request.statusText);
    }
});
}

Solution

  • Please remove static keyword from the web method you are calling. Then it will work.