使用Jquery将POST请求发送到REST端点

时间:2017-07-10 12:38:32

标签: javascript java jquery rest spring-mvc

我想从注册表单提交所有字段并将它们存储在PostgreSQL数据库中。我正在使用Spring MVC和jQuery。对/ save的POST请求仅使用Postman。当我点击注册按钮提交表单时,它会给我 -

  

“不支持请求方法'POST'。”

有人能帮助我吗?

用户控制器:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(method=RequestMethod.POST, value="/save", headers="Accept=application/x-www-form-urlencoded")
    public void registerUser(@RequestBody User user) {
        userService.registerUser(user);
    }
}

的PageController:

@Controller
public class PageController {

    @RequestMapping("/login")
    public String login () {
        return "index.html";
    }

}

型号:

@Entity
@Data
@Table(name="users")
public class User {

    @Id @GeneratedValue
    private long id;

    @Column(name="sex")
    private String sex;

    @Column(name="skype")
    private String skype;

    @Column(name="username")
    private String userName;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    protected User(){};
}

HTML:

<!doctype html>
<html>
<head>
<title>Login / Register</title>
<link 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" 
rel="stylesheet" type="text/css">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-
awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/login.js"></script>
</head>
<body>
<br>
<div class="row">
    <div class="container">
        <div class="login-register-form-section">
            <ul class="nav nav-tabs" role="tablist">
                <li class="active"><a href="#login" data-
 toggle="tab">Login</a></li>
                <li><a href="#register" data-toggle="tab">Register</a></li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane fade in active" 
 id="login">
                    <form class="form-horizontal" method="post" action="">
                        <div class="form-group " >
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-user"></i></div>
                                <input type="text" name="login_email" class="form-control" placeholder="Username or email" required="required" value="">
                            </div>
                        </div>
                        <div class="form-group ">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-key"></i></div>
                                <input type="password" name="login_password" class="form-control" placeholder="Password" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <input type="checkbox" id="rememberMe">
                            <label for="rememberMe">Remember Me</label>
                            <a href="#" class="pull-right">Forgot password?</a>
                        </div>  
                        <input type="submit" value="Login" class="btn btn-success btn-custom">
                    </form>
                </div>
                <div role="tabpanel" class="tab-pane fade" id="register">
                    <form class="form-horizontal" method="post" action="">
                        <div class="form-group" id="sex">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-user"></i></div>
                                <select type="text" name="gender" class="form-control">
                                <option>Male</option>
                                <option>Female</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group ">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-male"></i></div>
                                <input type="text" name="register_username" class="form-control" placeholder="Username" required="required" value="">
                            </div>
                        </div>
                        <div class="form-group ">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-envelope"></i></div>
                                <input type="email" name="register_email" class="form-control" placeholder="Email" required="required" value="">
                            </div>
                        </div>
                        <div class="form-group ">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-skype"></i></div>
                                <input type="text" name="register_skype" class="form-control" placeholder="Skype name" required="required" value="">
                            </div>
                        </div>
                        <div class="form-group ">
                            <div class="input-group">
                                <div class="input-group-addon"><i class="fa fa-lock"></i></div>
                                <input type="password" name="register_password" class="form-control" placeholder="Password" required="required">
                            </div>
                        </div>
                        <input type="submit" id="submitbtn" value="Register" class="btn btn-success btn-custom">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

jQuery的:

jQuery(document).ready(
function($) {

    $("#submitbtn").click(function(event) {

        var data = {}
        data["sex"] = $("#sex").val();
        data["skype"] = $("#register_username").val();
        data["username"] = $("#register_email").val();
        data["email"] = $("#register_skype").val();
        data["password"] = $("#register_password").val();

        $("#submitbtn").prop("disabled", true);

        $.ajax({
                 type: "POST",
                 contentType: "application/json",
                 url: "/save",
                 data: JSON.stringify(data),
                 dataType: 'json',
                 timeout: 600000,
                 success: function (data) {
                     console.log("DONE");
                 },
                 error: function (e) {
                     console.log("ERROR: ", e);
                     display(e);
                 }
        });


    });

});

3 个答案:

答案 0 :(得分:0)

而不是输入类型=&#39;提交&#39;你可以尝试使用输入类型=&#39;按钮&#39;

更改第data["username"] = $("#register_email").val(); to data["userName"] = $("#register_email").val();

答案 1 :(得分:0)

您在ajax呼叫中发送application/json,但在您的控制器中,您说您的终端只接受application/x-www-form-urlencoded

您可以将控制器更改为接受application/json

@RequestMapping(method=RequestMethod.POST, value="/save", headers="Accept=application/json")

为避免拼写错误,您还可以使用@RequestMapping的{​​{1}}属性,并将其传递给consumes常量:

MediaType

或者您可以重构您的ajax调用以发送序列化的表单数据。

正如enter image description here建议的那样,您可能还想将按钮的@RequestMapping(method=RequestMethod.POST, value="/save", consumes=MediaType.APPLICATION_JSON_VALUE) 属性更改为type,以避免默认的浏览器提交行为。或者,您可以在点击处理程序功能中调用button来完成同样的事情,但除非您真的想要使用浏览器,否则使用event.preventDefault();并没有多大意义#39;默认提交行为。

答案 2 :(得分:0)

好的,问题是我没有正确选择dom元素,而不是id =“email”我使用的是name =“email”。

<input type="text" name="register_skype" ...... >