2015-06-10 27 views
0

我有一个menubar.cshtml作为部分视图,其中我有一个名为navAllIssues的菜单项。在它的点击我调用我的angularjs控制器的GetUserProjects()方法名为IssuesController.But我geting 405(方法未发现错误)。angularjs发布错误-405(方法不允许)

误差图像

enter image description here

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

阿比控制器

[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

方法不允许的错误通常意味着HTTP方法(GET,POST,PUT或DELETE)不被允许。您的服务是否允许POSTS?你可以发布你的服务器端代码吗?

+0

您的POST请求请我已经发布我的API控制器代码太 – rupinder18

0

使用绝对url进行api调用,之前我使用的是相对url,这是因为它得到405(方法未找到)错误。

代码由绝对URL调用

var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetUserProjects/'; 
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添加这两条线

//网页API路线

变种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; 
    }