7

There's something about this that makes me feel slightly dirty, what's the appropriate way to pass values to the data field?

Currently I'm doing this: var jsonstring = "{ id: " + id + "}";

        <script type="text/javascript">
            function CompleteCB(id) {
                var jsonstring = "{ id: " + id + "}";
               
                $.ajax({
                    
                    type: "POST",
                    url: "/internal/completeholters.aspx/CompleteCB",
                    data: jsonstring,
                    contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    $("#row" + id).fadeTo("fast", 0.33);
                }
            });
            }

    </script>
2

2 Answers 2

8

leave it as an object and call JSON.stringify()

var obj = {};
obj.id = 22;

JSON.stringify(obj); // "{"id":22}" a JSON formated string
Sign up to request clarification or add additional context in comments.

1 Comment

You do it in one line: JSON.stringify({id: id}); - no need to explicitly create an object.
0

You can create the JSON directly, like so:

var query = { id: id };

Then in ajax call:

data: query,

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.