Invalid Web Service Call Error - JQuery Post to a WebMethod

Qamar, Mo 106 Reputation points
2022-03-31T16:13:00.68+00:00

188873-code.txt

I get the following error when posting to a WebMethod on a localhost running IIS 10. It works fine if it is run in Visual Studio.
Code is attached.

{"Message":"Invalid web service call, missing value for parameter: \u0027name\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

Internet Information Services
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 24,676 Reputation points
    2022-03-31T19:40:42.647+00:00

    The code you've shared has errors. I doubt it actually works in any environment. Most likely there is an issue with your test.

    The web method expects a name parameter but the first AJAX request does not send a name parameter.

    It is a bit baffling why you are attempting to send an image with jQuery AJAX then sending a semi-colon delimited string to the same method using XMLHttpRequest. Why not simply submit one properly formatted JSON request that has a base64 image and address information?


  2. AgaveJoe 24,676 Reputation points
    2022-03-31T21:38:03.8+00:00

    Again. the code you've shared has has two different HTTP requests one submits the image and the other submits a semicolon separated list. The screenshot cannot be executing the same code. My best guess is the deployed code is different than the code running in Visual Studio.

    0 comments No comments

  3. AgaveJoe 24,676 Reputation points
    2022-03-31T22:02:15.88+00:00

    Here's a basic example of submitting JSON to a web method.

    Model

        public class FormData
        {
            public DateTime date { get; set; }
            public int entityId { get; set; }
            public string email { get; set; }
            public string ssn { get; set; }
            public string address1 { get; set; }
            public string address2 { get; set; }
            public string headName { get; set; }
            public string landlordName { get; set; }
            public string landlordPhone { get; set; }
            public string phone { get; set; }
            public string image { get; set; }
        }
    

    Web Method

        [WebMethod(EnableSession = true)]
        public static FormData MyFunction(FormData formData)
        {
            return formData;
        }
    

    JavaScript

    $("#<%=btnSendAjax.ClientID%>").on('click', function (e) {
        e.preventDefault();
    
        var data = {
            formData: {
                date: '2022-03-31',
                entityId: 1,
                email: 'email@email.com',
                ssn: '123456789',
                address1: '123 West',
                address2: '',
                headName: 'name',
                landlordName: 'landlord',
                landlordPhone: '123654987',
                phone: '852369741',
                image: 'dsfasdfasdfwqwefasdadsf'
            }
        };
    
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "default.aspx/MyFunction",
            data: JSON.stringify(data),
        })
            .done(function (data) {
                console.log("success");
                console.log(data); 
            })
            .fail(function (jqXHR, textStatus, c) {
                console.log("failure");
                console.log(textStatus);
            });
    });
    
    0 comments No comments