【发布时间】:2020-07-20 11:51:59
【问题描述】:
我开发了一个基于 Web 的程序,在后端使用 java jersey,在前端使用 jsp。当我使用 Ajax 进行 post API 调用时,我的后端出现以下异常。
javax.json.stream.JsonParsingException: Unexpected char 117 at (line
no=1, column no=1, offset=0)
我猜我通过 Ajax API 调用传递的数据有问题。
这是我的 ajax API 调用:
var obj = JSON.parse('{ "userName":"John", "password":"hgvv", "img":"New York","fname":"kjbjk","lname":"bkbkkj","tp":"buhb","address":"jhbjhb","type":"user"}');
$.ajax({
type: "POST",
url: $url,
contentType: "application/json",
data: obj,
dataType: 'json',
success: function () {
alert("successed");
}
});
这是我的后端实现代码:
@Path("testing")
public class test {
UserRepository userRepo=new UserRepository();
@Path("users")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserModel CreateUser(UserModel a) {
userRepo.createUser(a);
return a;
}
}
【问题讨论】:
标签: java jquery json ajax jersey