Search code examples
c#javascriptjqueryajaxwebmethod

How to do ajax on this scenario?


javascript code

  function new_person_adder1() {

                var dataAjax = {
                    'entry_new_person_': $('#entry_new_person_1').val(),
                    'pop_new_person_identity_no': $('#pop_new_person_identity_no1').val(),
                    'identity_type_no': $('#select2_identity_type_bc_for_new_person_1').val()
                };
                jQuery.ajax({
                    url: 'WalkReserve.aspx/quick_person_entry',
                    type: 'POST',
                    data: JSON.stringify(dataAjax),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data.d);
                        if (data.d > 0) {
                            alert('Successfully Added');
                        } else {
                            alert('Failed to entry data!');
                        }
                    },
                    error: function (result) {
                        alert('Failed!');
                        console.log('Failed' + result.responseText);
                    }
                });
            }

C# code

 [WebMethod]
            public static string quick_person_entry(dynamic entry_new_person_, dynamic identity_type_, dynamic pop_new_person_identity_no)
            {
                return Convert.ToString(BAL.pop_quick_person_entry(entry_new_person_, identity_type_, pop_new_person_identity_no));
            }

ajaxdata enter image description here

i am getting following error

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

I want to send do ajax here but for some weird reason I am not able to do it. I am getting above error. can anybody point my mistake or help me?


Solution

  • Look at the parameters the method expects:

    entry_new_person_
    identity_type_
    pop_new_person_identity_no
    

    And the parameters being sent:

    entry_new_person_
    identity_type_no
    pop_new_person_identity_no
    

    The middle parameter is different. It's similar, but it's not the same thing. The model binder isn't smart enough (thankfully) to match things which are just similar. Changing the key in the JavaScript code should fix it:

    var dataAjax = {
        'entry_new_person_': $('#entry_new_person_1').val(),
        'pop_new_person_identity_no': $('#pop_new_person_identity_no1').val(),
        'identity_type_': $('#select2_identity_type_bc_for_new_person_1').val()
    };
    

    (Or changing the parameter name in the method would fix it too. Depends on which one is a bigger change. For example, if you have tons of JavaScript calls to this method then a single change to the method would be easier than many changes to the invocations of the method.)