使用Java,Ajax和json在Web服务上发布数据不起作用

时间:2013-02-11 17:43:44

标签: java jquery ajax json web-services

有人可以帮我解决这个问题。这总是出错,没有错误信息,只是console.log。有没有什么好的方法来调试这个,或者你能看到这个有什么问题吗?

谢谢! 萨米

function foo(){

        alert('Button pressed');
        var user={"id":10,"firstName":" Ted","lastName":"TESTING","age":69,"freeText":"55555","weight":55};

        $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "http://localhost:8080/testSoft/webresources/entity.user/",
        dataType: "json",
        data: user,

        success: function(data){alert(data);},
        error: function(data) {
        console.log('#####################error:################################'+data);                 
        alert('addUser error: ' + data.firstName);
        }
        });
        };

网络服务:

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(User entity) {
    super.create(entity);
}

用户

public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 45)
@Column(name = "firstName")
private String firstName;
@Size(max = 45)
@Column(name = "lastName")
private String lastName;
@Column(name = "age")
private Integer age;
@Lob
@Size(max = 65535)
@Column(name = "freeText")
private String freeText;
@Column(name = "weight")
private Integer weight;

1 个答案:

答案 0 :(得分:0)

PLS告诉我为什么这个功能在函数之外?如果我将函数调用插入到这样的按钮,它就不起作用了:

<button type="submit" class="btn btn-primary" id="btnAddUser" onclick="foo()">Register</button>

但是当它在函数之外时它正在工作:

<script>
            var user={"id":10,"firstName":" Coka","lastName":"CocaCola","age":69,"freeText":"55555","weight":55};
            alert('user----->' +user.firstName);
            $.ajax({

            type: "POST",
            contentType: "application/json",
            url: "http://localhost:8080/testSoft/webresources/entity.user/",
            dataType: "json",
            data: JSON.stringify(user),

            success: function(data){alert(data);},
            error: function(data) {
            console.log('#####################error:################################'+data);                 
            alert('addUser error: ' + data.firstName);
            }
            });
        </script>

我在JS中非常糟糕,我也讨厌它,现在更是如此:(