【问题标题】:Spring - 405 (Request method 'POST' not supported)Spring - 405(不支持请求方法“POST”)
【发布时间】:2014-06-10 16:43:57
【问题描述】:

我有通过 AngularJS 将表单数据传递给 spring 控制器的简单代码。在 chrome 调试器中,我可以看到请求传递了正确的值,但我收到一条错误消息,指出 405 (Request method 'POST' not supported) 并且我的控制器中的调试指针没有命中。所以我明白这是因为我的 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();

}

}

谁能帮我弄清楚我哪里出错了?

【问题讨论】:

  • 1.您是否在控制器中包含@Controller? 2. 为什么不在form 中使用action
  • 请检查 chrome 的控制台,看看被点击的 URL 是什么
  • value="/addemp" 应该是 value="IdeaOne#/addemp"

标签: javascript spring angularjs spring-mvc post


【解决方案1】:

在弹簧控制器中

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

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

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

【讨论】:

  • 是的,我更改了它,现在控制台给我一个错误消息“加载资源失败:服务器响应状态为 400(错误请求)”
  • 你还在url中使用'#'吗? '#' 表示转到一个命名的锚点,它不能在 url 的路径部分。
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 2020-08-12
  • 2020-07-24
  • 2017-05-03
  • 2016-06-16
  • 1970-01-01
  • 2012-06-24
  • 2015-05-01
相关资源
最近更新 更多