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>
kristianp
  • 5,496
  • 37
  • 56
Johnny Grimes
  • 411
  • 7
  • 17

2 Answers2

8

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

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

JSON.stringify(obj); // "{"id":22}" a JSON formated string
t3dodson
  • 3,949
  • 2
  • 29
  • 40
0

You can create the JSON directly, like so:

var query = { id: id };

Then in ajax call:

data: query,
kristianp
  • 5,496
  • 37
  • 56