我想發送一個JSON數據到我的控制器的POST處理程序。我這樣做,在我的客戶端:發送JSON數據到服務器錯誤
var userName = $('#userName').val();
var password = $('#password').val();
var mail = $('#mail').val();
var admin =$("#admin").is(':checked');
var user = {userName: userName, password: password, mail: mail, admin:admin};
$.ajax({
async : false,
type:'POST',
url: '/uxiy/webapp/uxmer',
data: user,
dataType: 'json',
success: function(data) {
...
},
error: function(data) {
...
}
});
我的春天控制器如下:
@RequestMapping(method = RequestMethod.POST)
public void createUser(HttpServletResponse response, @RequestBody User user) {
user.setName("POST worked");
//todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
response.setStatus(HttpServletResponse.SC_OK);
}
然而,當我把我的數據我得到這個錯誤在螢火蟲:
"NetworkError: 415 Unsupported Media Type"
是什麼錯誤?
PS: 的螢火蟲POST細節的例子:
Parameters application/x-www-form-urlencoded
admin true
mail [email protected]
password r
userName userx
Source
userName=userx&password=r&mail=user%40user.com&admin=true
PS2:當我加入
contentType: 'application/json',
它開始給
"NetworkError: 400 Bad Request"
可能是什麼問題,使序列化等?
PS3: 這裏:http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/它說:
If there are validation errors, a HTTP 400 is returned with the error messages, otherwise a HTTP 200 is returned.
我有400錯誤的請求錯誤。也許問題與此有關?
我跳這個職位將會使畫面清晰: http://stackoverflow.com/q/5908466/225396 –