开发者

How do I pass optional parameters into a WebMethod via JSON?

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 Crea开发者_Python百科teEvent(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 :(


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜