0

I've read several posts and questions yet all seem to make things more confusing than help. I am doing what seems pretty simple.

I have a button on basically a login form...

$("#beginButton").click(function () {
    if (validateStep(0) != false)
        goWizard(1);
});

that when clicked validates that three text fields are not empty, then passes the values to an API that "logs in" the user and returns a dataset with details about them.

function validateStep(step) {
    var isValid = false;
    $("#alertsPanel").html("").fadeOut();

    switch (step) {
        case 0:
            if ($.trim($("#login_LastName").val()).length == 0 || $.trim($("#login_BarNumber").val()).length == 0 || $.trim($("#login_BirthYear").val()).length == 0) {
                $("#alertsPanel").html("<strong>Error:</strong> All values are required.").fadeIn();
                return false;
            }
            else {
                $.ajax({
                    url: "/api/User/authenticateByLNameBYearBarNum",
                    //async: false,
                    cache: false,
                    data: {
                        lastName: $.trim($("#login_LastName").val()),
                        barNumber: $.trim($("#login_BarNumber").val()),
                        birthYear: $.trim($("#login_BirthYear").val())
                    },
                    dataType: "json",
                    error: function (response) {
                        $("#alertsPanel").html("<strong>Error:</strong> Member information not found.").fadeIn();
                        return false;
                    },
                    success: function (data) {
                        return true;
                    }
                });
            }
            break;

    }

    return isValid;
}

The first two checks work fine, I can validate the inputs are not empty and I can handle if the person is not found/returned by the API call. Where I am having the same issue as many others... I cannot correctly return the data to the calling function when the person is found. I get an object but it does not seem to contain the person details. I see the API does return the appropriate JSON.

Connie DeCinko
  • 996
  • 5
  • 19
  • 39
  • The referenced answer is not a duplicate as it is so damn confusing it would take me a week to read it all. Why the refusal to help? – Connie DeCinko Oct 31 '16 at 20:24
  • Calm down, take a step back, look at your code and think.... How long takes the AJAX to get the data from the server? **MUCH** longer than your code executes... Your code is **not** going to wait so long. It will execute immediately, not waiting for anything. What to do so? Catch and handle the server response at the time the response is there - in `success` callback. The referenced answer is well explained, take time and try to understand, otherwise you'll not be able to know what you're doing. – Artur Filipiak Oct 31 '16 at 20:36
  • Ok, I copied some of the sample code and now I can get the data out of the called function... but now I am not getting the returns of false when the person is not found. Do I need to modify what is returned to callback? Simply doing return false, never shows up in the browser. – Connie DeCinko Oct 31 '16 at 20:47
  • 1
    Take a look at the code - https://jsfiddle.net/10arbjmu/ – Artur Filipiak Oct 31 '16 at 21:01
  • @ArturFilipiak that looks really good so far. Making sense, instead of returning success/fail and values, you call a function that handles. I like how they all pass along true/false and string or data. So far so good! – Connie DeCinko Oct 31 '16 at 21:17
  • because `return` in AJAX won't do anything. The ONLY thing is returned from your `validateStep(0)` method is the `isValid` variable (which is `false`). AJAX takes longer to execute and `validateStep(0)` won't wait for anything you `return` from `success` / `error` callbacks. – Artur Filipiak Oct 31 '16 at 21:20

0 Answers0