locked
Ajax to read my json string RRS feed

  • Question

  • User1316246260 posted

    I have a subroutinr:

     [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 alert gives a message of "undefined". 

    How do I get Ajax to read my json string?

        <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>

    Sunday, February 22, 2015 11:11 AM

Answers

  • User1711366110 posted

    Hi philosophaie,
      As per your case, you can try like following code :

    <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: "{}",
                        async: true,
                        cache: false,
                        success: function (response) {
    // At this point, response is a *string* containing the
        //  manually generated JSON, and must be deserialized again.
     
        var gv;
     
        // This is a very common way of handling
        //  the second round of JSON deserialization:
        gv= eval('(' + response + ')');
     
        // You'll also see this approach, which
        //  uses browser-native JSON handling:
        gv= JSON.parse(response);
     
        // Using a framework's built-in helper
        //  method is another common fix:
        gv= $.parseJSON(gv);
    alert(gv); }, error: function (x, e) { alert("Error: " + x.responseText); } }); }); }); </script>

    Click here to know more about Manual JSON serialization

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2015 1:42 AM