locked
Ajax Jquery return in JSON format problem RRS feed

  • Question

  • User671453965 posted

    Below is my sample source code and would like to return data in JSON format. But the data return in string format "object object"? I wanted to return in JSON format. Which part that I made mistake?

     <script>
            function GetCar() {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetCar",
                    data: '{name: "' + $("#<%=txtCar.ClientID%>")[0].value + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            }
            function OnSuccess(response) {
                alert(response.d[0]);
            }
        </script>
    
     public class myCarList
        {
            public string Brand { get; set; }
            public string YearMade { get; set; }
            public string Quantity { get; set; }
        }
    
        [WebMethod]
        public static List<myCarList> GetCar(string name)
        {
            List<myCarList> carlist = new List<myCarList>();
            carlist.Add(new myCarList { Brand = "Toyota", Quantity = "120", YearMade = "2014" });
            carlist.Add(new myCarList { Brand = "Toyota", Quantity = "88", YearMade = "2013" });
            carlist.Add(new myCarList { Brand = "Honda", Quantity = "145", YearMade = "2009" });
            carlist.Add(new myCarList { Brand = "KIA", Quantity = "999", YearMade = "2011" });
    
            var query = (from carls in carlist
                         where carls.Brand == name.Trim()
                         select carls).ToList();
            return query;
        }

    Friday, May 13, 2016 8:22 AM

Answers

  • User-474980206 posted
    You set the dataType to json in the jquery call. This tells jquery to treat the response as json, and deserialize it to JavaScript object.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 13, 2016 6:03 PM
  • User61956409 posted

    Hi ckcheah,

    I agree with bruce. and if you’d like to convert JavaScript value to a JSON string, you could try to use JSON.stringify() method.

    function OnSuccess(response) {
        //alert(response.d[0]);
    
        alert(JSON.stringify(response.d[0]));
    }
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 9:09 AM

All replies

  • User632428103 posted

    Hello ckcheah,

    try this on your web method : 

     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    hope this help

    Friday, May 13, 2016 9:03 AM
  • User-474980206 posted
    You set the dataType to json in the jquery call. This tells jquery to treat the response as json, and deserialize it to JavaScript object.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 13, 2016 6:03 PM
  • User61956409 posted

    Hi ckcheah,

    I agree with bruce. and if you’d like to convert JavaScript value to a JSON string, you could try to use JSON.stringify() method.

    function OnSuccess(response) {
        //alert(response.d[0]);
    
        alert(JSON.stringify(response.d[0]));
    }
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 9:09 AM