0

I am new to Dropwizard and am having a little trouble with the conversion from POJO to Json. I am using jQuery at the frontend where I am calling the ajax function with the type POST. I am trying to validate a login against data in a MySQL database. 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.

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. However, the alert that is triggered on complete from the jQuery code prints an error.

The data returned from my server is valid JSon according to JsonLint. So it looks like ajax is not receiving the data which is strange because it sends the original login details successfully. Can anyone help with this?

Sparky
  • 98,165
  • 25
  • 199
  • 285
mcpolandc
  • 217
  • 2
  • 7
  • 15
  • well wouldn't you think that knowing what that error is would help? Try setting `dataType`. Also if you want to send JSON need to let ajax know since that isn't default contentType – charlietfl Feb 27 '15 at 17:04
  • @charlietfl In regards to the error... as you can see, I print the stringified data returned which prints {"readystate" : 0, "status" : 0, "statusText" : "error"} Also, setting the contentType causes my server code not to fire. I know this because I have set a breakpoint on the function. Setting the dataType also makes no difference. As in... contentType: 'application/json', dataType: 'json', – mcpolandc Feb 27 '15 at 17:17
  • Adding a proper error handler would give you more details. But you can also inspect the actual request in browser console network tab and see all parts of the request including headers, status, data sent/received – charlietfl Feb 27 '15 at 18:47
  • Hmmm... Now we're getting somewhere. The console says: XMLHttpRequest cannot load http://localhost:8080/User/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – mcpolandc Feb 27 '15 at 18:52
  • OK so is a cross domain request. Either need to enable CORS or make sure request is made to same port,protocol and sub domain. Even `www.domain.com` vs `domain.com` is considered cross domain request – charlietfl Feb 27 '15 at 18:55
  • Followed this http://stackoverflow.com/questions/25775364/enabling-cors-in-dropwizard-not-working and it seems to be working. Dropwizard is supposed to be easy! It has been a nightmare for me for the most part :/ – mcpolandc Feb 27 '15 at 19:13
  • Never edit title to say "solved", etc. Adding an answer and accepting it is more than adequate for marking something solved. Thanks. – Sparky Feb 28 '15 at 16:16

1 Answers1

0

Following the answer at Enabling cors in dropwizard not working seemed to fix my issue. It was a problem with CORS.

Community
  • 1
  • 1
mcpolandc
  • 217
  • 2
  • 7
  • 15