错误javax.json.stream.JsonParsingException:在进行AJAX API调用时发生

huangapple go评论81阅读模式
英文:

Error javax.json.stream.JsonParsingException: when making an AJAX API call

问题

我已经使用Java Jersey开发了一个基于Web的程序,后端使用了Java Jersey,前端使用了JSP。当我使用Ajax进行post API调用时,我的后端出现了以下异常。

javax.json.stream.JsonParsingException: 在 (行号=1, 列号=1, 偏移量=0) 处发现意外字符 117

我猜测问题可能出在通过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("成功");
    }
});

这是我实现的后端代码:

@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;
    }
}
英文:

I have developed a web based program using java jersey in my back end and jsp in my front end. When I make a post API call using Ajax my back end gets the following exception.

javax.json.stream.JsonParsingException: Unexpected char 117 at (line
no=1, column no=1, offset=0)

I guess it's something wrong with the data which I'm passing through the Ajax API call.

Here is my ajax API call:

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");

    }
});

This is my back end implemented code:

@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;
 }
}

答案1

得分: 1

你应该将数据发送为 JSON 字符串,而不是 JSON 对象。避免在你的代码中使用 JSON.parse

var data = '{ "userName":"John", "password":"hgvv", "img":"New York","fname":"kjbjk","lname":"bkbkkj","tp":"buhb","address":"jhbjhb","type":"user"}';

或者,我会构建一个 JavaScript 对象,然后在上面应用 JSON.stringify。这样,代码更易读:

var data = {
    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: JSON.stringify(data), // 在这里添加了 JSON.stringify
    dataType: 'json',
    success: function () {
        alert("successed");
    }
});
英文:

You should send the data as a JSON String, not a JSON Object. Avoid the JSON.parse from your code.

var data = '{ "userName":"John", "password":"hgvv", "img":"New York","fname":"kjbjk","lname":"bkbkkj","tp":"buhb","address":"jhbjhb","type":"user"}';

Alternatively, I would construct the JS Object, and apply JSON.stringify on it. This way, the code is more readable:

var data = {
    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: JSON.stringify(data), // added JSON.stringify here
    dataType: 'json',
    success: function () {
        alert("successed");
    }
});

huangapple
  • 本文由 发表于 2020年4月8日 14:19:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61094414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定