开发者

asp.net page method with jquery and parameter

开发者 https://www.devze.com 2023-02-11 19:42 出处:网络
In my javascript, I have: var testdate = \"{\'TheNewDate\' : \'12/02/2011\'}\"; $(\"#mydiv\").click(function () {

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 behind I have

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

However, when I click to run the call, I get the error function to activate. When I change the code behind function to pub开发者_如何学Pythonlic static string GetAppointement() it works. But I guess my goal is to pass a parameter to the code behind. What am I missing?

Thanks.


Your parameter is called DateInput and 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
    });
});


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

var testdate = "{'DateInput' : '12/02/2011'}";
0

精彩评论

暂无评论...
验证码 换一张
取 消