Create JSON string for AJAX

Something about this that makes me feel a little messy, what's the appropriate way to pass values ​​to a data field?

I am currently 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>

      

+3


source to share


1 answer


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



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

JSON.stringify(obj); // "{"id":22}" a JSON formated string

      

+3


source







All Articles