0

有人可以帮我解决这个问题。这总是出错,但没有错误消息,只是 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;
4

1 回答 1

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 很糟糕,我也讨厌它,现在更糟了:(

于 2013-02-11T20:20:02.310 回答