首页 > 解决方案 > ajax 结果数据为空

问题描述

我从控制器获取数据作为 json 结果。但在我的 ajax 中,数据是空的。为什么?

控制器:

public JsonResult GetMealType(string mType)
        {
            var obr = new Obroci();
            var obrGrid = obr.GetMealType(mType);



            return Json(obrGrid, JsonRequestBehavior.AllowGet);            
        }

Json 变量有值。: string:
[{"Type":"M1","Price":25,"Description":"Topli obrok"​​}]

阿贾克斯:

var newText = $('option:selected', this).text();

                            $.ajax({
                                url: "/Dumas/GetMealType?mtype=" + newText,
                                type: "POST",
                                data: 'json',
                                success: function (data) {
                                    alert(data.success);
                                    $("#lType").val(obj.Description);
                                },
                                error: function (status, error) {
                                    alert("An AJAX error occured: " + status + "\nError: " + error);
                                }
                            });

标签: jsoncontroller

解决方案


您必须将 Ajax 代码更正为:

    $.ajax({
            url: "/Dumas/GetMealType",
            type: "POST",
            data: JSON.stringify({ mtype: newText }),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert(data.success);
                $("#lType").val(obj.Description);
            },
            error: function (data) {
                alert("An AJAX error occured: " + status + "\nError: " + error);
            }
        });

推荐阅读