public ActionResult OrderDetails()
{
var data = new DataContainer();
ViewBag.OrderDetails = data.GetOrderDetails();
return Json(data);
}
以上是我的控制器。
public Detail GetOrderDetails()
{
return (from user in db.Users
join detail in db.OrderDetails
on user.Id equals detail.UserId
where (detail.DateAdded != null)
select new Detail
{
FirstName = user.FirstName,
Email = user.Email,
Gender = user.Gender,
Phone = user.Phone,
Destination = detail.DestinationCode,
CarrierCode = detail.CarrierCode,
OrderId = detail.OrderId
}).FirstOrDefault();
}
这是我的班级
这是我的ajax jquery方法
function doAjax(type, url, data, callback) {
$.ajax({
type: type,
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
callback(data);
},
failure: function (errMsg) {
alert(errMsg);
}
});
}
现在我想通过我的ajax jquery方法中的json检索我的控制器值。我怎样才能访问它们?
答案 0 :(得分:3)
将调试器语句放入JavaScript代码中。
像这样:function doAjax(type, url, data, callback) {
$.ajax({
type: type,
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
debugger;
callback(data);
},
failure: function (errMsg) {
alert(errMsg);
}
});
}
然后您可以观察控制器返回的对象。
答案 1 :(得分:0)
如果你是jquery代码正在命中你的action方法而服务器正在返回json,你可以像这样访问它。
function doAjax(type, url, data, callback) {
$.ajax({
type: type,
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.FirstName);
alert(data.Email);
alert(data.Phone);
callback(data);
},
error: function (a,b,c) {
alert(c);
}
});
}