10

I am developing asp.net mvc application, Java script will call controller for every 3 seconds and it should return list of json objects.I need to show the list of objects in table.Here, I am getting [object Object].should we deserialize that objects,if yes,then how to deserialize them.

Below is my java script code


<script>
    var fun = set_Interval(my_Timer, 3000);
    function my_Timer() {       
        $.ajax({           
            url: '@Url.Action("FirstAjax", "Home")',
            //data: '{param : "value"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: successFunc,
            error: errorFunc
        });
        function successFunc(response) { 
            alert(response);    
        }
        function errorFunc() {            
            alert('error');
        }
    }
</script>

below is controller

public JsonResult FirstAjax()
{            
    var listt = AlgoLegsClass.DataGridAlgos;
    JavaScriptSerializer js = new JavaScriptSerializer();
    string ss =  js.Serialize(listt);
    return Json(ss, JsonRequestBehavior.AllowGet);
}

Output- After every 3 seconds the timer call the controller and it returns list of objects.In alert box it is displaying like "Parameter name":"Value".How can i get these values as i need to append this list to table

0

1 Answer 1

4

Since you specified dataType: "json" in your ajax options, jQuery will already have done the deserialising of the JSON string back into a JavaScript object for you automatically.

What you're seeing is just what alert() does with JavaScript objects/arrays by default when trying to make them visual as text.

Try

alert(JSON.stringify(response));

instead. This will print a version of your object which is serialised back to a JSON string and thus make it more human-readable.

Or you can just look in the Response section of the ajax call's entry in your Network tab (in the browser's Developer Tools).

Also if you're using a JsonResult (i.e. return Json...) you do not need to serialise the object beforehand - MVC will do it for you. So chances are you're getting double-serialised nonsense.

Simply

return Json(listt, JsonRequestBehavior.AllowGet); 

should work no problem.

Sign up to request clarification or add additional context in comments.

13 Comments

I have changed my code as you specified above, I am getting output as [{"parameter _1":"value","parameter_2":"value","parameter_3":"value"}].
Ok. Well it's valid JSON, so that's good. What were you expecting to see? I don't know what's in your listt variable.
my list contains employee list, i need to get all employee details one by one with their fields like name,salary etc.
Ok but a description of your object is no good to anyone, a list of employees could have 100 different structures. Show the definition of whatever is the datatype of DataGridAlgos , and / or the actual values contained in this object at the time you run your code.
class Employee { Name {set;get;} Id {set;get;} salary {get;set;} . DataGridAlgos contains List of employees.Now we need to show all employee details one by one in alert box.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.