gpt4 book ai didi

javascript - 从 angularjs 文件调用 apicontroller 时未发现错误

转载 作者:行者123 更新时间:2023-11-29 21:28:19 25 4
gpt4 key购买 nike

我正在使用以下流程构建应用程序 我的 Controller 将调用 View 。在 View 中,我正在调用一个 js 文件,该文件具有 angularjs 代码并调用 APIController。

我收到以下错误:

Request URL:http://localhost:52096/api/EVerify/GetEmployeeList
Request Method:POST
Status Code:404 Not Found

我错过了什么或做错了什么。

Controller :

using System.Web.Mvc;

namespace MVC.EVerify.Controllers
{
[Authorize]
public class EVerifyController : Controller
{
#region Methods
#region ListEVerify
public ActionResult ListEVerify()
{
if (Request.IsAjaxRequest()) return PartialView();
return View();
}
#endregion
}

View :

在这个 View 中点击一个按钮,我正在加载另一个 View 以及 Javascript 文件,该文件具有名为 EverifyModule.js 的 Angular 代码

<table>
<tbody>
<tr ng-repeat="emp in EmployeeInfo">
<td>{{emp.name}}</td>
<td>{{emp.hireDate}}</td>
<td><a class="btn-sm btn-primary pull-right" href="javascript:void(0)" onclick="LoadViewSelected('/EVerify/EVerify/EVerifySubmit', 'EVerifyModule', 'E-VerifySubmit');">E-Verify</a></td>
</tr>
</tbody>
</table>

EVerifyModule.js

var EVerifyModule = angular.module('EVerifyModule', ['angularFileUpload', 'ui.bootstrap', 'angularUtils.directives.dirPagination']);


EVerifyModule.factory('EVerifyModuleService', ['$http', '$window', function ($http, $window) {

return {

GetEmployeeList: function (companyId) {
return $http({
url: '/api/EVerify/GetEmployeeList',
method: 'POST',
data: companyId
});
}
};
}]);


EVerifyModule.controller('EVerifyController', ['$scope', '$http', '$compile', 'EVerifyModuleService', '$modal', '$timeout', function ($scope, $http, $compile, EVerifyModuleService, $modal, $timeout) {

EVerifyModuleService.GetEmployeeList(58).then(function (response) {
$scope.EmployeeInfo = response.data.Employees;
});

EVerifyAPI Controller :

namespace MVC.EVerify.Controllers
{
[RoutePrefix("api/EVerify")]
public class EVerifyAPIController : ApiController
{
#region GetEmployeeList

[HttpPost]
[Route("GetEmployeeList")]
public async Task<IHttpActionResult> GetEmployeeList(int CompanyId)
{
List<EmployeeBO> employees = new List<EmployeeBO>();

try
{
employees = await EmployeeBL.GetEmployeeList(CompanyId);
}
catch
{
employees = new List<EmployeeBO>();
}

return Ok(new { Employees = employees });
}

#endregion

}
}

最佳答案

Model Binder 希望将您的 CompanyId 参数放在 URI 中,但您是在请求正文中发送它。

明确告诉您的操作方法您正在发送正文中的参数:

public async Task<IHttpActionResult> GetEmployeeList([FromBody] int CompanyId)

关于javascript - 从 angularjs 文件调用 apicontroller 时未发现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com