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

前端 未结 6 1924
孤街浪徒
孤街浪徒 2021-02-04 08:02

I need to get a list from mvc controller to view using jquery ajax. how can i do that. this is my code. Its alerting error message.

In Controller

 publ         


        
6条回答
  •  难免孤独
    2021-02-04 08:24

    The reason why i am not getting the result was.. I forgot to add json2.js in the library

     public class FoodController : Controller
        {
           [System.Web.Mvc.HttpGet]
            public JsonResult getFoodDetails(int userId)
            {
                IList FoodList = new List();
    
                FoodList = FoodService.getFoodDetails(userId);
    
                return Json (FoodList, JsonRequestBehavior.AllowGet);
            }
        }
    
    function GetFoodDetails() {
        debugger;
        $.ajax({
            type: "GET",
            url: "Food/getFoodDetails",
            data: { userId: Id },
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                debugger;
                alert(result)
            },
            error: function (response) {
                debugger;
                alert('eror');
            }
        });
    
    }
    

提交回复
热议问题