JQuery 不支持的媒体类型
JQuery Unsupported Media Type
我的服务器 REST Jsersey 收到 Json 和查询参数。我有 3 个查询参数:用户名、密码和电子邮件
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("register")
public Response register(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
System.out.println("username: " + userName);
System.out.println("password: " + password);
System.out.println("email: " + email);
if (TextUtil.isEmpty(userName) || TextUtil.isEmpty(password) || TextUtil.isEmpty(email)) {
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("Field should not be empty")).build();
}
List<User> listUser = findAll();
for(User user: listUser) {
if (user.getUsername().equals(userName)) {
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("This username is already registered"))
.build();
}
if (user.getEmail().equals(email)){
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("This email is already registered"))
.build();
}
}
我尝试让用户 Jquery 向服务器发送请求,但我收到 415 Unsupported media type
var signUp = function () {
$("#formSignUp").submit(function (e) {
// var userName = $('#textUserNameSignUp').val();
// var email = $('#textEmailSignUp').val();
// var password = $('#textPasswordSignUp').val();
e.preventDefault();
$.post('http://localhost:43319/BR/webresources/users/register',
{ username: "hello", password : "123", email : "email"},
function(returnedData){
console.log(returnedData);
})
.fail(function(){
console.log("error");
});
});
}
你能告诉我哪里出了问题并建议我如何正确处理吗
您需要为要发送的数据指定 contentType
尝试添加
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
在你signUp
函数
之前
我的服务器 REST Jsersey 收到 Json 和查询参数。我有 3 个查询参数:用户名、密码和电子邮件
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("register")
public Response register(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
System.out.println("username: " + userName);
System.out.println("password: " + password);
System.out.println("email: " + email);
if (TextUtil.isEmpty(userName) || TextUtil.isEmpty(password) || TextUtil.isEmpty(email)) {
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("Field should not be empty")).build();
}
List<User> listUser = findAll();
for(User user: listUser) {
if (user.getUsername().equals(userName)) {
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("This username is already registered"))
.build();
}
if (user.getEmail().equals(email)){
return Response.status(Response.Status.BAD_REQUEST)
.header("Access-Control-Allow-Origin", "*")
.entity(ErrorUtil.badRequest("This email is already registered"))
.build();
}
}
我尝试让用户 Jquery 向服务器发送请求,但我收到 415 Unsupported media type
var signUp = function () {
$("#formSignUp").submit(function (e) {
// var userName = $('#textUserNameSignUp').val();
// var email = $('#textEmailSignUp').val();
// var password = $('#textPasswordSignUp').val();
e.preventDefault();
$.post('http://localhost:43319/BR/webresources/users/register',
{ username: "hello", password : "123", email : "email"},
function(returnedData){
console.log(returnedData);
})
.fail(function(){
console.log("error");
});
});
}
你能告诉我哪里出了问题并建议我如何正确处理吗
您需要为要发送的数据指定 contentType
尝试添加
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
在你signUp
函数