0
我試圖用這個休息的方法來收到從數據庫中的用戶對象:在阿賈克斯發送對象獲取法的Javascript
@RestController
@RequestMapping("/rest/users")
public class UserRestController {
@Autowired
private UserService service;
@RequestMapping(method = RequestMethod.GET)
public Collection<User> getAllUsers(@RequestBody(required = false) User user) throws BadHttpRequest {
System.out.println(user);//ALWAYS RETURNING NULL :(
if (user == null) {
return service.getAllUsers(); //Always returns this!
} else if (user.getUserId() == null) {
return Collections.singletonList(service.getUserByEmail(user.getUsername()));//I want this
} else if(user.getUsername() == null){
return Collections.singletonList(service.getUserById(user.getUserId()));
}else{
throw new BadHttpRequest(new Exception("User id/email mismatch"));
}
}
我使用這個JavaScript:
var user = [{ "username": "[email protected]", "userId": 1}];
$.ajax({
type: "get",
url: "/rest/users/",
data: JSON.stringify({ User: user }),
success: function(data){alert(data);},
error: function(errMsg) {
alert("bais");
}
});
有誰想知道我做錯了什麼?
它沒有幫助。感謝您的嘗試! – Stav1
現在的錯誤是什麼?嘗試提供: contentType:「application/json」, 我做了同樣的事情,它似乎在我的項目中工作。 \t $阿賈克斯({ \t類型: 'POST', \t網址: 'JsonServlet', \t數據類型: 'Json的', \t JSON: 'Json的', \t數據:{發送:JSON.stringify( objc),name:namess,formid:value}, \t error:function(data){alert(「There was a problem in save the form」);}, \t success:function(data){alert(「Form保存併成功更新到數據庫「);} \t}); –