无法使用ajax从DB获取数据

时间:2022-06-30 15:30:00

I am developing an ASP.Net MVC application. I want to get data from database using ajax. It does not load data in web page and also there is no error on the console window and is no exception in visual studio. Following is my Controller code.

我正在开发一个ASP。净MVC应用程序。我想使用ajax从数据库中获取数据。它不加载web页面中的数据,控制台窗口也没有错误,在visual studio中也不例外。下面是我的控制器代码。

    [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());
        }
    }

This is the code in my view.

这是我认为的代码。

<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>';
                }
            }
        ]

    }
      );


}

I am unable to figure out what really i am doing wrong. Please someone help in this regard.

我不知道我到底做错了什么。在这方面请别人帮忙。

2 个解决方案

#1


1  

For Datatable You need Change TYPE to [HttpPost] & in you ajax change type "POST" As a developer you need add breakpoint to your controller & check its getting hit or not . Also you can console.log() / see result in browser developer tool

对于Datatable,您需要将类型更改为[HttpPost],而作为开发人员,您需要将ajax更改类型“POST”添加到控制器中并检查其是否命中。另外,您还可以在浏览器开发工具中使用console.log() /查看结果。

#2


1  

You use [HttpGet] in your action , means only allow http GET request in your action. But your ajax call you use POST request . That's not accept in your action. so change POST to GET if you need GET request OR change [HttpGet] to [HttpPost] for POST request .

您在操作中使用[HttpGet],意味着仅允许在操作中使用http GET请求。但是ajax调用使用POST请求。那是不接受的。因此,如果需要获取请求,请更改POST,或者将[HttpGet]更改为[HttpPost]以获取POST请求。

You do't need multiple ajax call.so need to change your ajax call because jquery datatable has own ajax call for data loading. you can fiend your answer this or this link. Or you can follow this YouTube video.

您不需要多个ajax调用。因此需要更改ajax调用,因为jquery datatable拥有用于数据加载的ajax调用。你可以用这个或这个链接来回答你的问题。或者你也可以看看这个YouTube视频。

#1


1  

For Datatable You need Change TYPE to [HttpPost] & in you ajax change type "POST" As a developer you need add breakpoint to your controller & check its getting hit or not . Also you can console.log() / see result in browser developer tool

对于Datatable,您需要将类型更改为[HttpPost],而作为开发人员,您需要将ajax更改类型“POST”添加到控制器中并检查其是否命中。另外,您还可以在浏览器开发工具中使用console.log() /查看结果。

#2


1  

You use [HttpGet] in your action , means only allow http GET request in your action. But your ajax call you use POST request . That's not accept in your action. so change POST to GET if you need GET request OR change [HttpGet] to [HttpPost] for POST request .

您在操作中使用[HttpGet],意味着仅允许在操作中使用http GET请求。但是ajax调用使用POST请求。那是不接受的。因此,如果需要获取请求,请更改POST,或者将[HttpGet]更改为[HttpPost]以获取POST请求。

You do't need multiple ajax call.so need to change your ajax call because jquery datatable has own ajax call for data loading. you can fiend your answer this or this link. Or you can follow this YouTube video.

您不需要多个ajax调用。因此需要更改ajax调用,因为jquery datatable拥有用于数据加载的ajax调用。你可以用这个或这个链接来回答你的问题。或者你也可以看看这个YouTube视频。