我的jQuery ajax函数总是返回错误,此错误消息为(常规故障)
我试图使用mvc中的另一个下拉列表绑定下拉列表中的数据。 当我从类别中选择时,子类别在控制器中的jsonresult操作中返回一个值,但ajax给了我一个错误General Failure,我不知道错误在哪里 这是控制中的代码我的jQuery ajax函数总是返回错误,此错误消息为(常规故障),jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我试图使用mvc中的另一个下拉列表绑定下拉列表中的数据。 当我从类别中选择时,子类别在控制器中的jsonresult操作中返回一个值,但ajax给了我一个错误General Failure,我不知道错误在哪里 这是控制中的代码 public ActionResult Create() { List<sub_categories> subCategoryList = new List<sub_categories>();
public ActionResult Create()
{
List<sub_categories> subCategoryList = new List<sub_categories>();
// Select List Category
var dropdownListCategoryEnr = new SelectList(db.category.ToList(), "cat_id", "cat_name_en");
ViewBag.cat_id = dropdownListCategoryEnr;
//Select List Sub category
ViewBag.sub_cat_id = new SelectList(subCategoryList, "sub_cat_id", "sub_name_en");
return View();
}
[HttpGet]
public JsonResult GetSubCategoryById(string categoryId = "")
{
List<sub_categories> subCategoryList = new List<sub_categories>();
int ID = 0;
if(int.TryParse(categoryId, out ID))
{
subCategoryList = db.sub_categories.Where(x => x.cat_id.Equals(ID) && x.is_deleted == false).ToList();
}
if(Request.IsAjaxRequest())
{
return new JsonResult
{
Data = subCategoryList,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
return new JsonResult
{
Data = "Not Valid request",
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
这是jquery代码:-
$(document).ready(function () {
$("#cat_id").change(function () {
// this will call when Country Dropdown select change
var categoryId = parseInt($("#cat_id").val());
if (!isNaN(categoryId)) {
var ddsub = $("#sub_cat_id");
ddsub.empty(); // this line is for clear all items from State dropdown
ddsub.append($("<option></option").val("").html("Select State"));
// Here I will call Controller Action via Jquery to load State for selected Country
$.ajax({
url: "@Url.Action("GetSubCategoryById", "Products")",
type: "GET",
data: { categoryId: categoryId },
dataType: "json",
success: function (data) {
$.each(data, function (i, val) {
ddsub.append(
$("<option></option>").val(val.sub_cat_id).html(val.sub_name_en)
);
});
},
error: function (xhr, ajaxOptions, errorThrown) {
alert(xhr.responseText);
}
});
}
});
});
试试看
if (Request.IsAjaxRequest())
{
return Json(subCategoryList, JsonRequestBehavior.AllowGet);
}
else
{
return Json("Not Valid Request", JsonRequestBehavior.AllowGet);
}
我创建了一个类似的示例,其中根据父下拉列表中选择的值填充下拉列表。请试一试
<div class="box box-frame" style="padding-bottom: 10px;">
<div class="box-inner" style="padding-left: 0px">
<div class="box-caption">Search Reports</div>
<div style="padding: 2px;">
@Html.ValidationSummary()
</div>
<ul class="piped font-weight-normal">
<li>
<b>Company: </b>
@Html.DropDownListFor(m => Model.CompanyId,
Model.CompanyLookupValues.Select(
c => new SelectListItem() { Value = c.CompanyId.ToString(), Text = c.CompanyName }
),
"-- select --",
new { @class = "width-4",onchange = "GetReportTypes();" })
@Html.ValidationMessageFor(m => Model.CompanyId)
<text> </text>
<b> Report Type: </b>
@Html.DropDownListFor(m => Model.ReportId,
Model.ReportLookupValues.Select(c => new SelectListItem() { Value = c.ReportId.ToString(), Text = c.ReportName }),
"-- select --",
new { @class = "width-4" })
@Html.ValidationMessageFor(m => Model.ReportId)
<input class="button-primary" type="button" id="btnGetReportFilters" value=" Get Report Filters " onclick="GetReportFilters();" />
</li>
</ul>
</div>`enter code here`
</div>
Jquery代码
function GetReportTypes() {
var companyId = $("#CompanyId").val();
if (companyId != '') {
var url = '@Url.Action("GetReportTypes", "Report")';
$.getJSON(url + '?companyId=' + companyId, function (data) {
var select = $("#ReportId");
select.empty();
select.append($('<option/>', {
value: '',
text: "-- select --"
}));
$.each(data, function (index, itemData) {
select.append($('<option/>', {
value: itemData.ReportId,
text: itemData.ReportName
}));
});
});
}
}
MVC代码
public JsonResult GetReportTypes(int companyId)
{
List<Report> reportTypes = new List<Report>();
if (companyId > 0)
{
// for admin, return all report types
if (IsAdministrator)
{
reportTypes = ReferenceDataService.GetReportTypes(true).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList();
}
else
{
reportTypes = ReferenceDataService.GetReportTypes(false).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList();
}
}
return Json(reportTypes, JsonRequestBehavior.AllowGet); ;
}
添加此行时:-db.Configuration.ProxyCreationEnabled=false;很好,为什么伙计们,这行代码用的是什么?当您提出AJAX请求时,您应该能够在Chrome developer tools F12中检查它,这可能会显示错误消息。这里没有任何错误subCateogryList给我值,但AJAX代码给我一般的失败
public JsonResult GetReportTypes(int companyId)
{
List<Report> reportTypes = new List<Report>();
if (companyId > 0)
{
// for admin, return all report types
if (IsAdministrator)
{
reportTypes = ReferenceDataService.GetReportTypes(true).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList();
}
else
{
reportTypes = ReferenceDataService.GetReportTypes(false).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList();
}
}
return Json(reportTypes, JsonRequestBehavior.AllowGet); ;
}