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: 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) …

via Asp.Net Developed Tutorials » Search Results » ajax:

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:
I then use it in my page like so:
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 :(
………………………………….

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.

For more info: How do I pass optional parameters into a WebMethod via JSON?

Asp.Net Developed Tutorials » Search Results » ajax

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

Share this post:

Related Posts

Leave a Comment