简体   繁体   English

Dropwizard / Jackson从函数返回HTTP代码200但AJAX POST获取错误

[英]Dropwizard / Jackson returns HTTP code 200 from function but AJAX POST gets error

I am new to Dropwizard and am having a little trouble with the conversion from POJO to Json. 我是Dropwizard的新手,从POJO到Json的转换有点麻烦。 I am using jQuery at the frontend where I am calling the ajax function with the type POST. 我在前端使用jQuery,我使用POST类型调用ajax函数。 I am trying to validate a login against data in a MySQL database. 我正在尝试验证MySQL数据库中的数据登录。 I know the database is connected and the correct user can be located in the database based on the credentials entered on the HTML login page. 我知道数据库已连接,并且可以根据HTML登录页面上输入的凭据在数据库中找到正确的用户。

Here is the backend code: 这是后端代码:

    @POST
@UnitOfWork
@Path("/login")
@Produces("application/json")


public User login(String credentials) {

    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readValue(credentials, JsonNode.class);
        String  username = root.get("Username").toString().replaceAll("\"", "");
        String password = root.get("Password").toString().replaceAll("\"", "");

        User user = userDAO.findByUsername(username);

        //Check password matches and respond
        boolean passwordMatched = user.isPasswordMatched(password);

        //Return correct response
        if (passwordMatched) {
            return user;
        }
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return null;

}

Then at the front end I simply post the username and password to the url: 然后在前端我只是将用户名和密码发布到网址:

    $(document).ready(function() {

    $("#login").click(function() {

        var username = $("#username").val();
        var password = $("#password").val();

        $.ajax({

            url:'http://localhost:8080/User/login',
            type: 'POST',
            data: JSON.stringify({"Username" : username, "Password" : password}),
            complete: function(data, status) {
                alert("Status: " + status + "Data: " + JSON.stringify(data));
            }

        });

The console output from the server is HTTP code 200 and when I set a breakpoint in the login function I can see the correct database entry being found and being returned from function. 服务器的控制台输出是HTTP代码200,当我在登录功能中设置断点时,我可以看到找到正确的数据库条目并从函数返回。 However, the alert that is triggered on complete from the jQuery code prints an error. 但是,从jQuery代码完成时触发的警报会输出错误。

The data returned from my server is valid JSon according to JsonLint . 根据JsonLint,从我的服务器返回的数据是有效的JSon So it looks like ajax is not receiving the data which is strange because it sends the original login details successfully. 所以看起来ajax没有收到奇怪的数据,因为它成功发送原始登录详细信息。 Can anyone help with this? 有人能帮忙吗?

Following the answer at Enabling cors in dropwizard not working seemed to fix my issue. 启用cow in dropwizard中的答案后, 工作似乎无法解决我的问题。 It was a problem with CORS. 这是CORS的一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM