2014-04-25 63 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的路徑部分。 –

相關問題