Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON string for AJAX

Tags:

json

jquery

ajax

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>
like image 209
Johnny Grimes Avatar asked May 22 '15 00:05

Johnny Grimes


People also ask

Can you use JSON with AJAX?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.

How pass JSON data to AJAX to controller?

Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.

What does JSON parse () method do when we initiate an AJAX request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.


1 Answers

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

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

JSON.stringify(obj); // "{"id":22}" a JSON formated string
like image 86
t3dodson Avatar answered Nov 04 '22 15:11

t3dodson