我有两个执行两个 ajax 调用的函数。如果我调用一个函数而不是另一个函数,则代码将正常执行。但是,如果我同时调用它们,我会收到两条内部服务器错误消息。我假设的每个功能都有一个。
这是我的代码:
$(document).ready(function(){
CategoryChangeState(@Model.CatId , subcategoryId);
SubategoryChangeState(@Model.SubcatId);
})
public ActionResult ReturnListOfSubcategories( FormCollection collection ) {
string categoryId = collection["result"];
var subcategories = ProductManagerHelperClass.ReturnSubcategories(categoryId);
return Json(subcategories);
}
public ActionResult ReturnListOfBrands() {
var brands = ProductManagerHelperClass.ReturnBrands();
return Json(brands);
}
function CategoryChangeState(value , editPage) {
.....
$.ajax({
type: "POST",
url: "/ProductManager/ReturnListOfSubcategories",
data: { result: value },
datatype: "json",
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
...
}
function SubategoryChangeState(value) {
....
$.ajax({
type: "POST",
url: "/ProductManager/ReturnListOfBrands",
datatype: "json",
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
....
}
为什么我会收到这些错误,我该如何解决?
编辑
在调试时我发现在这部分代码中:
public static Dictionary<string , string> ReturnSubcategories(string categoryId)
{
int catId = int.Parse(categoryId);
var subcategories = (from s in dataContext.SubCategories
where s.CatId == catId
select new
{
s.SubCatId,
s.SubCatName
}).ToDictionary(x => x.SubCatId.ToString(), x => x.SubCatName);
return subcategories;
}
linq 查询抛出异常:
InvalidOperationException ExecuteReader requires an open and available Connection. The connection's current state is closed.
同样,仅当我同时调用两个函数时才会引发此异常