我从控制器获取数据作为json结果。 但是在我的ajax中,数据为空。为什么?
控制器:
public JsonResult GetMealType(string mType)
{
var obr = new Obroci();
var obrGrid = obr.GetMealType(mType);
return Json(obrGrid, JsonRequestBehavior.AllowGet);
}
Json变量具有值。:
字符串:
[{“ Type”:“ M1”,“ Price”:25,“ Description”:“ Topli obrok”}]
ajax:
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);
}
});
答案 0 :(得分:0)
您必须将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);
}
});