Asp.net page way with jquery and parameter

In my javascript, I have:

var testdate = "{'TheNewDate' : '12/02/2011'}"; $("#mydiv").click(function () { $.ajax({ type: "POST", url: "../Pages/Appointments.aspx/GetAppointements", data: testdate, contentType: "application/json; charset=utf-8", dataType: "json", success: successFn, error: errorFn }); }); 

In my code I have

 [WebMethod] public static string GetAppointements(string DateInput) { var t = DateInput; 

However, when I click to start a call, I activate the error function. When I change the code behind the function to the public static string GetAppointement (), it works. But I assume that my goal is to pass the parameter to the code. What am I missing?

Thanks.

+4
source share
2 answers

Your parameter is called DateInput , not TheNewDate , so:

 $('#mydiv').click(function () { $.ajax({ type: 'POST', url: '../Pages/Appointments.aspx/GetAppointements', data: JSON.stringify({ dateInput: '12/02/2011' }), contentType: 'application/json; charset=utf-8', dataType: 'json', success: successFn, error: errorFn }); }); 
+4
source

You must make the JSON data match the parameter name in the web service method.

 var testdate = "{'DateInput' : '12/02/2011'}"; 
+2
source

Source: https://habr.com/ru/post/1339894/


All Articles