0

I have a following situation

function Insert(name, parentID) {

            $.ajax({
                type: "POST",
                url: "topic.aspx/Insert",
                data: JSON.stringify({
                    "name": name,
                    "parentID": undefined
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {


                },
                error: function (msg) { alert(msg.responseText); }


            });
            }

And C# looks like this

[WebMethod]
        public static int Insert(string name, int? parentID)
        {
            GM.KnowledgeBase.Business.Topics topics = new GM.KnowledgeBase.Business.Topics();
            return topics.Insert(name, parentID);
        }

When parentID is null or undefined I get "Invalid web service call, missing value for parameter". Is there a more elegant way to solve this issue other then passing a parameter as a string and detecting if string is empty?

2
  • 4
    Don't pass the parameter at all? Commented Jun 9, 2014 at 12:31
  • Good idea, but the param is not passed if it is null. JSON.stringify does not include it in json output. So the solution is in modifying input params in C# for method Insert(). Commented Jun 9, 2014 at 12:43

2 Answers 2

2

Don't pass it when it's not defined?

function Insert(name, parentID) {

    var data = { name: name };

    if(parentID)
        data.parentID = parentID

        $.ajax({
            type: "POST",
            url: "topic.aspx/Insert",
            data: data,
            // etc...
       });
}

or if your webmethod can parse null, this should do:

data: JSON.stringify({
    "name": name,
    "parentID": parentID || null
}),
Sign up to request clarification or add additional context in comments.

6 Comments

it will be expected. maybe creating override method without parendID? is that what you are saying?
@John Since it's nullable, try my latter example.
Actually when value is null, JSON.stringify will not include this param in json output. So the solution is to create additional c# method something like public static int Insert(string name). This works
@John JSON.stringify inclues properties with null values. console.log(JSON.stringify({ "name": 'blah', "parentID": null }));
try console.log(JSON.stringify({ "name": 'blah', "parentID": undefined})); i think it is up to jquery version
|
0

Since you declared the parameter as nullable (in Insert function) you can safely do not send it from ajax function

$.ajax({
                type: "POST",
                url: "topic.aspx/Insert",
                data: JSON.stringify({
                    "name": name

                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {


                },
                error: function (msg) { alert(msg.responseText); }


            });
            }

After this you'll see a null value in Insert function for parentID parameter

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.