Returning Json object from controller action to jQuery

前端 未结 4 1239
[愿得一人]
[愿得一人] 2020-12-31 05:43

I\'m attempting to get this working properly (2 days now). I\'m working on a log in where I\'m calling the controller action from jQuery, passing it a JSON object (utilizing

4条回答
  •  自闭症患者
    2020-12-31 06:18

    If you're using MVC 2, you have to return something like this :

    return Json(your_object, JsonRequestBehavior.AllowGet);
    

    I've found it here

    For a different usage, here is my code.

    JQuery :

    $(document).ready(function () {
        $("#InputDate").live('click', function () {
            var date = $("#InputDate").val();
            if (date != "") {
                $.getJSON("/Home/GetNames",
                        { date: $("#InputDate").val() },
                        function (data) {
                            $("#ProviderName").empty();
                            // [...]
                            });
                        });
            }
        });
    });
    

    And C#

    public JsonResult GetNames(string date)
     {
       List list = new List();
       // [...]
       return Json(list, JsonRequestBehavior.AllowGet);
    }
    

提交回复
热议问题