2014-04-25 70 views
0

我有简单的代码,通过AngularJS将表单数据传递给弹簧控制器。在Chrome调试器中,我可以看到正确的值正在传递请求,但是我收到一个错误,说405 (Request method 'POST' not supported),并且我的控制器中的调试指针没有命中。所以我明白这是因为我的POST请求没有映射到我的控制器,但我已经做了一切正确的事情。Spring - 405(请求方法' POST '不支持)

形式: -

<form name="empForm" ng-controller="insertEmpCtrl" ng-submit="insertEmp()"> 
<table> 
    <tr><td>First name: <input type="text" class="form-control" name="fname" ng-model="formData.fname"/></td></tr> 
    <tr><td>Last name: <input type="text" class="form-control" name="lname" ng-model="formData.lname"/></td></tr> 
    <tr><td>Contact no: <input type="text" class="form-control" name="contact" ng-model="formData.contactno"/></td></tr> 
    <tr> 
     <td><input type="submit" value="Save" /> <input type="reset" value="Edit"></td> 
     <td></td> 
    </tr> 
</table> 

AngularJS控制器: -

empApp.controller('insertEmpCtrl',function($scope,$http){ 

     $scope.insertEmp = function(){ 


      $http.post("http://localhost:8080/IdeaOne#/addemp", $scope.formData, { 
       withCredentials: true, 
       headers: {'Content-Type': 'application/json'}, 
       transformRequest: angular.identity 
      }).success(function(){console.log("done")}).error(function(){console.log("error")}); 

     }; 
    }); 

弹簧控制器: -

public class HelloController { 

@RequestMapping(method = RequestMethod.GET) 
public String home(ModelMap model){ 

    return "hello"; 
} 

//method redirects to hello.jsp 
@RequestMapping(value="/addemp",method = RequestMethod.POST) 
public String addEmployee(@RequestBody Employee employee) { 


    EmployeeManager.empDB.add(employee); 

    return "hello"; 
} 


//method returns a link list as a json doc to the front end 
@RequestMapping(value="/viewall",method = RequestMethod.GET) 
public @ResponseBody 
LinkedList<Employee> viewAll(ModelMap model){ 


    return EmployeeManager.viewEmployees(); 

} 

}

有人可以帮我弄清楚我哪里出错了吗?

+0

1.您是否在控制器中包含'@ Controller'? 2.为什么不在你的'form'中使用'action'? – jhyap

+0

请检查铬的控制台,看看是什么URL被击中 – geoand

+0

值=“/ addemp”应该是值=“IdeaOne#/ addemp” –

回答

0
在弹簧控制器

@RequestMapping(value="/addemp",method = RequestMethod.POST)

将路径设置为/addemp。但js代码发布到/IdeaOne#/addemp。 (我不熟悉angularjs,我不知道url中的'#'符号的含义)。

您必须确保两条路径相同。

+0

是我改变了它,现在控制台给我一个错误说:“无法加载资源:服务器响应状态为400(错误请求)“ – helloworld

+0

你仍然在URL中使用'#'。 '#'表示转到一个命名的锚点,它不能在url的路径部分。 –

相关问题