json - 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);
}
});
解决方案
您必须将 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);
}
});
推荐阅读
- nscursor - 如何创建带有 alpha 掩码的自定义 NSCursor?
- android - 我的 Flutter 应用程序中的 Google 登录失败
- mongodb - 如何在从其他集合 mongodb 嵌入的数组中选择具有 id 的集合
- javascript - 简单每秒点击一个按钮事件
- jsonschema - 当父属性不存在时阻止依赖属性验证
- ios - 如何从拖放创建新的 UIWindowScene?
- jquery - 弹出窗口未触发 focusout() 或 blur()
- clojure - 文件名无效,得到 xxxxxx 但应为 xxxxxxxx
- python - 如何从 exec() 越狱?
- javascript - 在组件之间传递值:传递引用与主题