我有一个menubar.cshtml作为局部视图,其中我有一个名为navAllIssues的菜单项。在它的点击我调用我的angularjs控制器的GetUserProjects()方法名为IssuesController.But我正在调整405(找不到方法错误)。
错误图片
menubar.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>();
}
答案 0 :(得分:0)
方法不允许错误通常意味着不允许使用HTTP方法(GET,POST,PUT或DELETE)。您的服务设置是否允许POSTS?你能发布服务器端代码吗?
答案 1 :(得分:0)
使用绝对URL来调用api,之前我使用的是相对url,因为它获得了405(找不到方法)错误。
通过绝对网址调用的代码
var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetUserProjects/';
答案 2 :(得分:0)
我遇到了类似的问题,经过长时间的研究后,我找到了一个适合我的简单解决方案。
删除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);
和你的角色
的帖子请求 Factory.postData = function (data) {
var request = $http({
method: "POST",
url: urlBase+'api/url',
data: data,
headers: {
"Content-Type": "application/json"
}
});
return request;
}