简体   繁体   中英

How to get a <list> from mvc controller to view using jquery ajax

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);
    }

您正在从ajax响应中获取一个对象,以查看警报中如何尝试实例化您的对象: alert(JSON.stringify(data))

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM