我正在開發一個ASP.Net MVC應用程序。我想用ajax從數據庫中獲取數據。它不會在網頁中加載數據,並且在控制檯窗口上也沒有錯誤,並且在Visual Studio中也不例外。 以下是我的控制器代碼。無法使用ajax從數據庫獲取數據
[HttpGet]
public JsonResult GetCompanies()
{
try
{
IEnumerable<Company> company = _companyService.GetCompanies().ToList();
IEnumerable<CompanyListViewModel> viewModelListCompanies = Mapper.DynamicMap<IEnumerable<Company>, IEnumerable<CompanyListViewModel>>(company);
return new JsonSuccessResult(viewModelListCompanies);
//return Json(accountTypes, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
return new JsonErrorResult(ex.ToString());
}
}
這是我認爲的代碼。
<div class="row">
<div class="col-md-9">
<div class="block">
<div class="header">
<h2>Sortable table</h2>
</div>
<div class="content">
<table cellpadding="0" cellspacing="0" width="100%" class="table table-bordered table-striped sortable" id="myDbTable">
<thead>
<tr>
<th data-hide width="20%">ID</th>
<th data-hide width="20%">Name</th>
<th data-hide width="20%">E-mail</th>
<th data-hide width="20%">Phone</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
}
@section Scripts{
<script>
var DataColumnsCount = 4;
//***Start Fatching Data for datatable *** start //
$.ajax({
type: 'POST',
url: @Url.Action("GetCompanies","Company"),
dataType: 'json',
data: "{}",
success: fetchCompanyTableList});
function fetchCompanyTableList() {
$('#myDbTable').DataTable({
ajax: {
url: @Url.Action("GetCompanies", "Company"),
type: "GET",
dataType: "json",
dataSrc: 'DataSet',
},
"columns": [
{"data" : "Id"},
{ "data": "Name" },
{ "data": "Email" },
{ "data": "Owner" }
],
"aoColumnDefs": [
{
"aTargets": [DataColumnsCount],
"mData": null,
"bSortable": false,
"mRender": function (data, type, fullRow) {
return '<th width="20%"><a href="#">Details</a></th>';
}
}
]
}
);
}
我無法弄清楚什麼我真的做錯了。請有人在這方面提供幫助。
你得到什麼迴應Ajax請求? – CyberAbhay
它給出:狀態碼200 OK – Ahmad
和控制器的GetCompanies方法未找到。這意味着它沒有得到我的控制器GET代碼。現在我應該在這裏做什麼? – Ahmad