2017-08-17 57 views
1

當我使用ajax調用'POST'將簡單表單​​提交給我的數據庫時,它將空值保存到我的數據庫中。我使用Spring引導,Spring Data,PostgreSQL。我試圖修復這個小時,但無法找到任何東西。我的代碼有什麼問題?通過Ajax提交表單不工作,Spring控制器中的數據爲空

.............................................. .................................................. .................................................. ............

型號:

package english.chat.app.model; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

import lombok.Data; 

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

@Id @GeneratedValue 
private long id; 

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

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

protected User(){}; 
} 

控制器:

package english.chat.app.controller; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.MediaType; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 

import english.chat.app.model.User; 
import english.chat.app.services.UserService; 

@RestController 
public class UserController { 

@Autowired 
private UserService userService; 

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

} 

存儲庫:

package english.chat.app.repo; 

import java.util.List; 

import org.springframework.data.repository.CrudRepository; 

import english.chat.app.model.User; 

public interface UserRepo extends CrudRepository<User, Long> { 
} 

HTML:

<!DOCTYPE html> 
<html> 
<head> 
<title>My App</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script> 
<script type="text/javascript" src="js/app.js"></script> 
</head> 
<body> 
<form id="form"> 
    <input type="email" name="email"> 
    <input type="password" name="password"> 
    <button type="submit" id="butt">Create</button> 
</form> 
</body> 
</html> 

JS:

$(document).ready(function() { 

$("#form").submit(function (event) { 

    //stop submit the form, we will post it manually. 
    event.preventDefault(); 

    fire_ajax_submit(); 

}); 

}); 

function fire_ajax_submit() { 

var search = {}; 
search["password"] = $("#password").val(); 
search["email"] = $("#email").val(); 

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

$.ajax({ 
    type: "POST", 
    contentType: "application/json", 
    url: "http://localhost:8080/save", 
    data: JSON.stringify(search), 
    dataType: 'json', 
    cache: false, 
    timeout: 600000, 
    success: function (data) { 
    }, 
    error: function (e) { 
    } 
}); 

} 

回答

0

您未正確選擇DOM元素,#password口令意味着你選擇的元素與ID密碼,相同的#電子郵件,所以更新您的HTML相應,你應該是ABL e堅持你的用戶。

<input type="email" name="email" id="email"> 
<input type="password" name="password" id="password"> 
+0

是的,你是對的,愚蠢的錯誤,謝謝。 – Michael