4

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 public static string GetAppointement() it works. But I guess my goal is to pass a parameter to the code behind. What am I missing?

Thanks.

2 Answers 2

4

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
    });
});
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, it was just too easy! But it can take a while to figure out on your own.... thanks a lot.
@frenchie, also notice the usage of JSON.stringify instead of hardcoding strings which won't be properly encoded.
Quick question: the variables that I declared in my code behind page are not available in the page method; is this normal?
@frenchie, yes, that's perfectly normal. Static methods such as PageMethods should be have only access to static class members.
I have the userID that I need to access in the page method. For now it's just a number that's hard coded but later it'll be the real user's ID. How do I work around this in the page method? How do I authenticate the incoming request to belong this a particular user?
|
2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.