Codette
Codette

Reputation: 1007

How to read json value of a response in jquery

This is the script part of one of my widgets in a website.Anyway when textboxes of "inp" class has a changed value the code sends all the parameters to a webservice which looks through database and return records as json.My problem is i dont know how to get values in json response.I'm trying to add new divs to res12 div as results which should come from the json.I was able to create the divs but values are undefined and also the script runs for multiple times and i dont understand why.I need your help.thanks in advance. the json response

[{"description":"sent for a meeting","date":"19.02.2012 21:34:26","lid":"6","companyname":"M&L","personname":"jack ferrel","email":"jacky@ml.com","fax":"123123","phone":"345345","industryname":"finance","teamname":"w12et","sender":"jack ferrel","statusname":"waiting"}]

   <script type="text/javascript">

          var typingTimer;               
          var doneTypingInterval = 5000;
          $(document).ready(function () {
              $(".inp").keyup(function () {
                    typingTimer = setTimeout(doneTyping, doneTypingInterval);
                });
                $(".inp").keydown(function () {
                    clearTimeout(typingTimer);
                });

            });

            function doneTyping() {
                $.ajax({
                    type: "POST",
                    url: "letter.ashx",
                    data: { CompanyName: $("#<%=TextBox1.ClientID%>").val(), PersonName: $("#<%=TextBox3.ClientID%>").val(), Email: $("#<%=TextBox5.ClientID%>").val() },
                    dataType: "application/json",
                    success: function (msg) {

                        var newdiv = $('<div class="e"><div></div><div>'+CompanyName+'</div><div>'+Sender+'</div><div>1</div><div>2</div></div> ');

                        $('#res12').append(newdiv);
                    }
                });
            }
        </script>

Upvotes: 0

Views: 606

Answers (1)

ShankarSangoli
ShankarSangoli

Reputation: 69915

You are seeing undefined in the new divs created because CompanyName and Sender are not defined. If they are part of ajax response the you use msg.CompanyName and msg.Sender. Try this.

        function doneTyping() {
            $.ajax({
                type: "POST",
                url: "letter.ashx",
                data: { CompanyName: $("#<%=TextBox1.ClientID%>").val(), PersonName: $("#<%=TextBox3.ClientID%>").val(), Email: $("#<%=TextBox5.ClientID%>").val() },
                dataType: "application/json",
                success: function (msg) {

                    var newdiv = $('<div class="e"><div></div><div>'+msg.CompanyName+'</div><div>'+msg.Sender+'</div><div>1</div><div>2</div></div> ');

                    $('#res12').append(newdiv);
                }
            });
        }

Update:

The json you posted in the question has companyname and not CompanyName, same for sender. Remember that JavaScript is case sensitive so you have to use companyname and sender in your code to get there values.

Upvotes: 2

Related Questions