views:

285

answers:

2

I have the following JSON class I am intending to use to perform management calls on my database asynchronously:

<script type="text/javascript">

    var CalendarManager = {

        defaultOptions: {
            staffcode: 0,      // required
            date: 0,           // required
            activityCode: 0,   // required
            clientCode: null,  // optional
            contactCode: null, // optional
            destination: '',   // optional/doesn't matter
            travelCode: null,  // optional
            miles: null,       // optional
            overnight: false,  // optional
            notes: ''          // optional/doesn't matter
        },

        createEvent: function(options) {
            var combinedOptions = $.extend(true, {}, this.defaultOptions, options);
            $.ajax({
                type: "POST",
                url: 'calendar/calendar-manager-ajax.aspx/CreateEvent',
                data: combinedOptions,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data, textStatus, XMLHttpRequest) {
                    alert(textStatus + ", " + data);
                },
                error: function(data, textStatus, XMLHttpRequest) {
                    alert(textStatus + ", " + data);
                }
            });
        }
    };
</script>

I then use it in my page like so:

<script type="text/javascript">
    CalendarManager.createEvent(); // random test
</script>

At the moment, I'm trying to get this AJAX call fire the following method:

[WebMethod()]
public static string CreateEvent(int staffcode, int date, 
    int activitycode, int? clientcode,
    int? contactCode, string destination, 
    int? travelcode, int? miles, 
    bool overnight, string notes)
{
    return null;
}

Unfortunately, the method CreateEvent isn't getting called, and I get an AJAX error (setting a breakpoint doesn't stop execution):

error, [object XMLHttpRequest]

If I change data: combinedOptions to data: "{}" and add another method to my ASPX (as below) the code works successfully:

[WebMethod()]
public static string CreateEvent()
{
    return null;
}

Basically, my question is: how do I specify optional parameters to a WebMethod when providing JSON data?

I know I can reduce the parameters down to just the required values, and then use HttpContext.Request.Params to read the values of optional paramaters, but I would have thought the way I have tried here should have worked.

EDIT

The XMLHttpRequest.responseText value for the error is:

Invalid JSON primitive: staffcode.

This is throwing me even more off the scent of the problem :(

+1  A: 

There are no such things as 'optional' parameters. Pass a default for unused params.

Sky Sanders
That's what I'm trying to do - `null` is used in the defaults, so the `int?` values would hopefully contain `null`. All parameters have been specified as you can see.
Codesleuth
looks like you are already at the place you need to be. There really isn't a way to do what you suggest. I know, I have tried. You cannot have optional parameters and if you construct the endpoint with no parameters in the hopes of reading them from the context you will get an exception for unrecognized params. IIRC.
Sky Sanders
+1  A: 

Fixed it. There were 2 problems.

Firstly, the case of all my parameters was incorrect (note "activityCode" not "activitycode").

And I also now use this:

data: JSON.stringify(combinedOptions)

Silly errors really, but there you have it.

Codesleuth