Search code examples
jqueryasp.net-ajaxasmx

AJAX call for ASMX web method not working


I am trying to invoke a web method (ASMX service) using ajax call, below is my ajax call:

$.ajax({
    type: "POST",
    url: "/License.asmx/SubmitDatas",
    data: jsonString,
    Type: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var data = response;
        console.log(data);
        $('#monitordata').append(JSON.stringify(data))
    },
    failure: function (response) {
        console.log(response);
    }
});

Backend ASMX service:

[WebMethod]
public string SubmitDatas(SubmitData submitData)
{

}

Note: SubmitData is my custom model class

ERROR:

System.InvalidOperationException: SubmitDatas 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)

When I try to post, the above exception is thrown.


Solution

  • If your asmx file is in the root, try removing the first slash from url (not sure if it matters).

    Spell Type with a small t. I'll have to assume the jsonString is ok. (doesn't seem relevant.)

    Note that your method parameter accepts the type SubmitData

    public string SubmitDatas(SubmitData submitData)
    

    but you are sending a string. Your method parameter should be

    [WebMethod]
    public string SubmitDatas(string submitData) // <-- string.
    {
    
    }
    

    Also, fyi, Datas is a common mistake. Datas is not an english word; data is plural of datum. In english, datas is never used. A common mistake for ESL.