Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a subroutine:
C#
[WebMethod]
       public static string gvAjax()
       {
           List<Table1> gvlist = new List<Table1>();
           ...
           var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
           var json1 = serializer.Serialize(gvlist).ToString();
           return json1;
       }


I get a string from the WebMethod of the form
"[{"Id":1,"col1":"11","col2":"22","col3":"33"},"{"Id":2,"col1":"44","col2":"55","col3":"66"},"{"Id":3,"col1":"77","col2":"88","col3":"99"}]"

The json string exists in string form by sending the subroutine to a textbox.

The alert gives a message of "undefined".

How do I get Ajax to read my json string?
C#
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#<%= Button2.ClientID %>").click(function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "/Default.aspx/gvAjax",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({}),
                async: true,
                cache: false,
                success: function (data) {
                    alert(data.d);
                    ...
                },
                error: function (x, e) {
                    alert("Error: " + x.responseText);
                }
            });
        });
    });
</script>
Posted
Updated 22-Feb-15 7:16am
v2

1 solution

That is because you first need to convert the JSON data to an Object in JavaScript. Once you convert it to an Object, then you can call this property. The data that comes from your web server is in the form of JSON, and which would be considered as a string on the client-side; JavaScript.

As it is, the data object (because it is considered as a plain-variable; more like a string and it) doesn't have any member d in it. That is why JavaScript shows undefined when you try to display the values.

For example the following code,

JavaScript
success: function (data) {
   var obj = JSON.parse(data); 
   alert(obj.d);
}


One last thing, the JSON data that you're having is an array containg 3 objects, so it would be good to first get the object at index, and then check for the property. Something like this.

JavaScript
var obj = JSON.parse(data);
// Get the first object
var firstObj = obj[0];
// There is a property Id not d.
alert(firstObj.Id);


You can always learn more about JSON notation in browsers; W3.Org standards, from Mozilla Developer Network.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify[^]

Or you can at the same time learn the JSON syntax to learn more on how objects are serialized and deserialized and how the members are treated from the JSON.org[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-15 13:59pm    
5ed.
—SA
Afzaal Ahmad Zeeshan 22-Feb-15 14:48pm    
Thanks, Sergey.
Santosh K. Tripathi 22-Feb-15 23:29pm    
5+
Afzaal Ahmad Zeeshan 23-Feb-15 0:03am    
Thanks, Santosh.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900