不从C#Controller Action返回AJAX成功
我尝试了很多可用的选项,但它仍然没有将数据返回给AJAX成功可能是什么原因?
我想返回列表Listemr_t_Immunization
从控制器操作到我的成功AJAX调用数据我应该怎么做
请参阅以下代码谢谢
AJAX请求
$.ajax(
{
url: '@Url.Action("getmedi", "Immunization")',
type: 'Post',
async: false,
contentType: 'application/json',
dataType: "json",
data: JSON.stringify({ "patient2": patient2}),
success: function (data) {
debugger
$(data).each(function(index, element){
$('#first > tbody').append('<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>');
$('#first > tbody > tr:last-child > td:nth-child(1)').append('<input id='+element.ImmunizationId+' value='+element.VaccineName+'>');
$('#first > tbody > tr:last-child > td:nth-child(2)').append('<input id='+element.Immunization_Id+' value='+element.DateGiven+'>');
$('#first > tbody > tr:last-child > td:nth-child(3)').append('<input id='+element.Immunization_Id+' value='+element.Route+'>');
})
$("input[name='VaccineName']").attr('disabled', 'disabled');
$("input[name='DateGiven']").attr('disabled', 'disabled');
$("input[name='Route']").attr('disabled', 'disabled');
},
error: function (textStatus, errorThrown)
{
debugger
}
});
控制器操作
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx = (emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
///List<emr_t_Medical_History> Listemr_t_Medical_History2 = (from Allemr_t_Medical_History in db.emr_t_Medical_History where Allemr_t_Medical_History.Mr_No == Mr_No select Allemr_t_Medical_History).ToList();
if (erx != null)
{
//return Json(new { success = true, for_Date = erx.Med_Date, for_Name = erx.Name, for_Active = erx.Active, for_Resolved = erx.Resolved, for_Comments=erx.Comments });
return Json(new { Listemr_t_Immunization, JsonRequestBehavior.DenyGet });
}
}
return Json(new { success = false });
}
我还试图在返回之前对其进行字符串化并显示以下错误
我已经习惯了
return JsonConvert.SerializeObject(Listemr_t_Immunization);
现在它给我错误
使用类型检测到自引用循环 &#39; System.Data.Entity.DynamicProxies.as_t_appointment_305685F58BEE75BAC95975F9457412A0DE58BB59D3EDBD1155C7DB5E21A7BA66&#39 ;. 路径 &#39; [0] .erx_t_patient.as_t_appointment [0] .erx_t_city.as_t_appointment&#39;
答案 0 :(得分:0)
只需快速浏览一下,看起来您可以将失败返回到其他语句中以保护它们不会被解雇,除非您的某个条件失败,而不是仅仅自行浮动。
您的回叫每次都失败的问题是什么?如果是这样可能是原因。
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx = (emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
///List<emr_t_Medical_History> Listemr_t_Medical_History2 = (from Allemr_t_Medical_History in db.emr_t_Medical_History where Allemr_t_Medical_History.Mr_No == Mr_No select Allemr_t_Medical_History).ToList();
if (erx != null)
{
//return Json(new { success = true, for_Date = erx.Med_Date, for_Name = erx.Name, for_Active = erx.Active, for_Resolved = erx.Resolved, for_Comments=erx.Comments });
return Json(new { Listemr_t_Immunization, JsonRequestBehavior.DenyGet });
} else
{
return Json(new { success = false });
}
} else
{
return Json(new { success = false });
}
}
另外在旁注中,处理API-ish成功和失败的更好方法是使用HTTP错误代码。
Response.StatusCode = (int)HttpStatusCode.Created;
return Json(new { results });
修改
在您的请求中发回一个列表。
这一行需要改变:
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
我猜这个db是你的上下文对象,如果是这样,用以下代码替换该行:
var results = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList()
我不是100%确定erx是什么,但是如果你检查你有结果,那么将其更改为。
if (results.Any())
{
Response.StatusCode = (int) HttpStatusCode.Created;
return Json(new { results });
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
所以你的代码都会读到:
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx =(emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
var results = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList()
/// You can make that query easier to read by having
/// var results = db.emr_t_Immunization.Where(a => a.Patient_Id == patient2).ToList()
if (results.Any())
{
Response.StatusCode = (int) HttpStatusCode.Created;
return Json(new { results });
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
}