1

I need to get a list from mvc controller to view using jquery ajax. But I get [object Object].

Ajax code

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '/Home/getList',                        
            success: function (data) {
                debugger;
                $scope.StuList = data;
                alert(data);                                
            }
        });

In Controller

    public JsonResult getList()
    {
       Models.clsSave obj = new Models.clsSave();    
       var list = new List<Model>();
       list= obj.getList();
       return Json(list,JsonRequestBehavior.AllowGet);
    }
2
  • Instead of alert(data) use console.dir(data) and look in the browser console window. The alert window will only give you a type (in this case object) unless it's a simple type (string/int etc). Commented Sep 18, 2015 at 10:27
  • The answer is that you are already getting the data as a list, but looking at it incorrectly. Commented Sep 18, 2015 at 10:34

3 Answers 3

1

If you are getting [Object,Object] means it contains the data. You just Iterate it on the success function of your $.ajax as shown below

success: function (data) {
$.each(data.items, function(item) {
            alert('id :'item.id +' Name:'+item.sname);
            });
}

All the best

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

1 Comment

$.each(data, function (i, item) This is whats worked for me.
0

You are getting an Object from ajax response to view that in alert try to stingify your object: alert(JSON.stringify(data))

Comments

0

I need to get a list from mvc controller to view using jquery ajax. But I get [object Object].

that means you're getting correct data list

how will I store it in $scope.StuList

You can just assign that data to $scope.StuList.

$scope.StuList = data;

Just to verify your data, put it in iteration

$.each($scope.StuList,function(i,v){
  alert(v.sname);
});

You should get the correct data.

Or you can also put it in console, and verify yourself.

console.log($scope.StuList)

Hope this will help, or let me know if you still facing any issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.