-3

I'm having a MVC project where I have a Json call to the controller. This call returns nothing, even thou it always should return either true or false.

My code looks like this

Register.cshtml

function registera() {
    var $email = $('#register-email'),
        $password = $('#register-password'),
        $repeatPassword = $('#register-repeatPassword'),
        $firstname = $('#register-firstname'),
        $lastname = $('#register-lastname'),
        $message = $('#message-register');

    if ($password.val() != $repeatPassword.val()) {
        $message.className = 'alert alert-danger';
        $message.html("Both passwords must be identical");
    } else {
        $message.className = 'alert';
        showLoadingText($message);

        register($email.val(), $password.val(), $firstname.val(), $lastname.val(), function (data) {
            if (data.IsValid()) {
                $message.html('');
                $message.className = '';
            } else {
                $message.className = 'alert alert-danger';
                $message.html(data.Message());
            }
        });
    }
};

script.js

function register(email, password, firstname, lastname) {
    $.get("/Account/GetJson_Register", { email: email, password: password, firstname: firstname, lastname: lastname }, function (data) {
        return data;
    }, 'json');
};

AccountController.cs

public ActionResult GetJSON_Register(string email, string password, string firstname, string lastname)
{
    repository.Register(email, password, firstname, lastname);

    return Error.Instance.Message != ""
        ? Json(new { IsValid = false, Message = Error.Instance.Message })
        : Json(new { IsValid = true, Message = Error.Instance.Message });
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89
  • 1
    `return` inside the callback isn't the same as return from function even if you could due to asynchronous nature of ajax. Your function returns nothing – charlietfl Sep 27 '14 at 21:51

1 Answers1

1

It makes little sense to "return" from the asynchronous success-callback in this context. In this case looks like the register function is missing an additional parameter for it's own callback function - it isn't that the "data is empty" so much as the appropriate function is never invoked.

Compare with:

function register(email, password, firstname, lastname, callback) {
    $.get("/Account/GetJson_Register", { email: email, password: password, firstname: firstname, lastname: lastname },
          function (data) {
            // Invoke the callback, supplying the relevant result
            callback(data);
          }, 'json');
};

Do note that the above code uses a wrapper in a silly fashion to show a point. Consider if it were written like the following, where the user-code provided callback is still invoked on-success. This works because neither the parameters nor the function context needed to be changed.

function register(email, password, firstname, lastname, callback) {
    $.get("/Account/GetJson_Register", { email: email, password: password, firstname: firstname, lastname: lastname },
          callback, 'json');
};

Of course, the unified method is to use Promises instead of manual callback wrapping. In this case, because $.get already returns a promise the code could be written like so:

function register(email, password, firstname, lastname) {
    // Return $.Promise returned from $.get as created by the wrapped $.ajax..
    return $.get("/Account/GetJson_Register",
                 { email: email, password: password, firstname: firstname, lastname: lastname },
                 'json');
};

function showError (message) {
    $message.className = 'alert alert-danger';
    $message.html(message);
}

register($email.val(), $password.val(), $firstname.val(), $lastname.val())
    .done(function (data) {
        if (data.IsValid) {
            $message.html('');
            $message.className = '';
        } else {
            // This should be .Message and not .Message(), etc.
            showError(data.Message);
        }
    })
    // It is also good to handle cases when a non-200 OK occurs
    .fail(function (xhr, message) {
        showError("Registration failed: " + message);
    });
user2864740
  • 60,010
  • 15
  • 145
  • 220