我有一个 menubar.cshtml 作为部分视图,其中有一个名为 navAllIssues 的菜单项。单击后,我正在调用名为 IssuesController 的 angularjs 控制器的 GetUserProjects() 方法。但是我收到 405(方法未找到错误)。
错误图片
菜单栏.cshtml
<li id="navAllIssues" data-ng-controller="allIssuesController">
<a class="auto hideover" data-ng-click="GetUserProjects()">
<span class="pull-right text-muted">
<i class="i i-circle-sm-o text"></i>
<i class="i i-circle-sm text-active"></i>
</span>
<i class="i i-file-copy i-2x icon"></i>
<span class="font-bold">All Issues</span>
</a>
</li>
IssuesController.js
$scope.GetUserProjects = function() {
//var isMobileView = false;
//$scope.issueCount = -1;
alert('test1');
$scope.issuesLoaded = false;
$scope.issueDetailsLoaded = false;
var windowWidth = $(window).width();
if (windowWidth < 768) {
isProjectSelected = false;
$("#projectContainer").show();
$("#email-content").hide();
$("#issueContainer").hide();
selectedTab = "project";
}
$("#busyIndicator").show();
$scope.query = "";
var url = 'api/Issues' + '/GetUserProjects/';
$http.post(url, []).success(function(data, status, headers, config) {
if (data != '' || data.length == 0) {
if (data != '' || data.length == 0) {
$scope.Projects = data;
$scope.projectIssuesStatus = $scope.Projects.ProjectIssues;
$scope.Projects = $filter('orderBy')($scope.Projects, 'Name');
alert("test--"+$scope.Projects[0].Name);
if ($scope.selectedProject == null) {
$scope.selectedProject = $scope.Projects[0];
$scope.target = $scope.selectedProject.ID;
if ($location.search().Project != undefined) {
$scope.target = $location.search().Project;
for (var countp = 0; countp < $scope.Projects.length; countp++) {
if ($scope.Projects[countp].ID == $scope.target) {
$scope.selectedProject = $scope.Projects[countp];
}
}
}
} else {
for (var count = 0; count < $scope.Projects.length; count++) {
if ($scope.Projects[count].Id == $scope.selectedProject) {
$scope.selectedProject = $scope.Projects[count];
}
}
}
if ($scope.selectedProject.MyIssueCount > 0 || $scope.selectedProject.OpenIssueCount > 0) {
$scope.showIssues($scope.selectedProject);
}
// if (!isMobileView) {
// $scope.showIssues($scope.Projects[0]);
// }
} else {
$scope.selectedProject = null;
}
} else {
$scope.errors.push(data.error);
}
});
if ($scope.isVisible == false) {
$("#changedetailsbox").hide();
$scope.isVisible = true;
}
if ($scope.isVisibleReply == false) {
$("#postReplybox").hide();
$scope.isVisibleReply = true;
}
};
API控制器
[HttpPost]
[AuthenticationRequired]
public List<ProjectBO> GetUserProjects()
{
var userId = SessionItems.UserId;
var result = BLL.PublicLayer.GetUserProjects(userId);
return result.IsSuccessful ? result.Result : new List<ProjectBO>();
}
Method not allowed 错误通常意味着不允许使用 HTTP 方法(GET、POST、PUT 或 DELETE)。您的服务设置是否允许 POSTS?你能发布你的服务器端代码吗?
使用绝对url来调用api,之前我使用相对url,因为这导致了405(找不到方法)错误。
通过绝对url调用的代码
var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetUserProjects/';
我遇到了类似的问题,经过长时间的研究,我找到了一个对我有用的简单解决方案。
删除 web.config 中所有与 CORS 相关的设置
喜欢
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
只需在 WebAPIconfig.cs 中添加这两行即可
// Web API 路由
var cors = new EnableCorsAttribute("", "", "*"); config.EnableCors(cors);
以及您来自 Angular 的帖子请求
Factory.postData = function (data) {
var request = $http({
method: "POST",
url: urlBase+'api/url',
data: data,
headers: {
"Content-Type": "application/json"
}
});
return request;
}