简体   繁体   English

jQuery不向Spring控制器发送数据

[英]Jquery doesn't send data to Spring controller

I have a javaEE application and i have a jquery form to add a user to database. 我有一个JavaEE应用程序,我有一个将用户添加到数据库的jquery表单。 I send fiekd values to my Spring controller, but for some reason the data isn't sent. 我向我的Spring控制器发送了怪异的值,但是由于某种原因未发送数据。 What is the issue? 有什么问题

I get the following exception in log 我在日志中收到以下异常

java.lang.IllegalStateException: Could not find @PathVariable [login] in @RequestMapping

Form 形成

<div id="dialog-form" title="Create new user">
<p class="validateTips">All form fields are required.</p>
<form:form action="${pageContext.request.contextPath}/test" method="post">
    <fieldset>
        <label for="login">Login</label>
        <input type="text" name="login" id="login" class="text ui-widget-content ui-corner-all" />
        <label for="password">Password</label>
        <input type="text" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />            
        <label for="email">Email</label>
        <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
 </fieldset>
</form:form>
</div>
<button id="create-user">Add user</button>

Script 脚本

$( "#dialog-form" ).dialog({
            autoOpen: false,
            height: 600,
            width: 350,
            modal: true,
            buttons: {
                "Create an account": function() {
                    var login = $( '#login' );
                    var email = $( '#email' );
                    var  password = $( '#password' );
                    $.ajax({
                                type: "POST",
                                url: "${pageContext.request.contextPath}/test",
                                data: "login=" + login + "&email=" + email + "&password="+ password,
                                success: function(){
                                    $( this ).dialog( "close" );
                                }
                    })

Controller 控制者

@RequestMapping(value="/test", method=RequestMethod.POST)
@ResponseBody
public void test(@PathVariable String login, @PathVariable String password, @PathVariable String email) {
    User user = new User();
    user.setLogin(login);
    user.setPassword(password);
    user.setEmail(email);
    userService.create(user);
}

@PathVariable is for information embedded in the URL 'REST Style'. @PathVariable用于嵌入在“ REST风格” URL中的信息。 eg,: 例如,:

@RequestMapping(value="/test/user/{login}/password/{password}/email/{email}", method=RequestMethod.POST)

To read in regular old http parameters that are in the query string or post body the annotaton is @RequestParam , rather than @PathVariable . 要读入查询字符串或帖子正文中的常规旧http参数,注释应为@RequestParam而不是@PathVariable

public void test(@RequestParam("login") String login, @RequestParam("password") String password, @RequestParam("email") String email) {

Also in your ajax, data is expecting, well, data. 同样,在您的ajax中, data也在期待数据。 If you want to format it as query string, it should be appended to the URL directly. 如果要将其格式化为查询字符串,则应将其直接附加到URL。 to put it in the body you would do: 将其放入体内,您将执行以下操作:

data: {login:login ,email: email,password: password}

rather than format it as a query string. 而不是将其格式化为查询字符串。 You can also just use jQuery's .serialize() method on the form tag to easily send the entire form as if the submit button had been clicked. 您还可以仅在form标记上使用jQuery的.serialize()方法来轻松发送整个表单,就好像单击了提交按钮一样。

Since it is POST request you have to use @RequestParam instead of @PathVariable 由于它是POST请求,因此您必须使用@RequestParam而不是@PathVariable

@PathVariable is for GET requests @PathVariable用于GET请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM