0

I am posting to my database (for context using GAE, and Objectify as a DAO) and it posts correctly (and the backend returns a response of 202), however, under the Ajax it is not calling the "success" block (i.e. in the method below even when it post's correctly, it calls alert("error1")). The source code for Ajax says a Post is supposed to call the success block when the status is between 200 and 300. Any ideas why it isn't working? Any help would be great!

function userExist() {
        var rootUrl = "http://localhost:8888/api/";
        function loginToJSON() {
              return JSON.stringify({
                                    "username": $('#username').val(),
                                    "password": $('#password').val()
                });
        }
        //System.out.println(loginToJSON());
        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: rootUrl + 'userLogin',
            dataType: "json",
            data: loginToJSON(),
            success: function(data, status, jqXHR) {
                alert("success");
            },
            error: function(jqXHR, status, errorThrown){
                alert("error1");
            }
        });
    }

The relevant java backend is

@POST
public Response login(Login l) {
    if (loginService.checkCred(l)) {
        return Response.status(202).build();
    } else {
        return Response.status(403).build();
    }
}
user123429
  • 187
  • 1
  • 8
  • 1
    Does `Response` return valid `JSON`? – guest271314 Jan 22 '17 at 05:03
  • @user123429, that would be my guess too. Either the response is invalid `JSON` or something failed after writing to the database on the server. @guest271314, I was just checking out your profile. Can you link to a resource explainging `((89.99999999999999 !== 90.00000000000000) !== (89.999999999999999 !== 90.000000000000000))`? – Raphael Rafatpanah Jan 22 '17 at 05:06
  • @RaphaelRafatpanah What question do you have? – guest271314 Jan 22 '17 at 05:09
  • @guest271314, I'm just curious as to why that statement evaluates to true (within my browser's console). – Raphael Rafatpanah Jan 22 '17 at 05:10
  • 1
    @RaphaelRafatpanah In general, see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken/588014#588014) – guest271314 Jan 22 '17 at 05:17
  • In this case, I don't think Response is returning JSON (i.e why there is no .entity(object) method). Why does it need to be? In other HTTP methods that do return JSON I am using Jackson to serialize objects and the respective annotations and am having no issues. – user123429 Jan 22 '17 at 05:17
  • @user123429 Have you checked the console for errors? If the console is reporting a server error, have you checked the server logs? – Raphael Rafatpanah Jan 22 '17 at 05:20
  • @user123429 Have you tried without `contentType: 'application/json'`? – guest271314 Jan 22 '17 at 05:21
  • The console is not reporting a server error. The only thing the console is reporting is `Jan 21, 2017 8:40:48 PM com.google.appengine.api.datastore.dev.LocalDatastoreService$11 run INFO: Time to persist datastore: 11 ms` – user123429 Jan 22 '17 at 05:22
  • @user123429 Does not appear to be server error, but `JSON.parse()` error reading response. – guest271314 Jan 22 '17 at 05:25
  • @guest271314 Yes I have, doesn't fix it. – user123429 Jan 22 '17 at 05:25
  • @guest271314 hmm can you expand on your comment? It is pulling the data out of the JSON correctly. – user123429 Jan 22 '17 at 05:27
  • @user123429 What does `return Response.status(202).build()` return? What is `JSON` response to `$.ajax()` call? – guest271314 Jan 22 '17 at 05:31
  • @user123429 If you are not serving or expecting a `JSON` response you can include `processData:false` at `$.ajax()` setting. – guest271314 Jan 22 '17 at 05:37
  • `return Response.status(202).build()` just returns the status. Their is no JSON being returned and `processData:false` unfortunately that didn't work. Although it did make the web console unhappy. It said `POST http://localhost:8888/api/userLogin 415 (Unsupported Media Type)` – user123429 Jan 22 '17 at 05:43

0 Answers0