$.GetJson数据为空

本文关键字:数据 GetJson | 更新日期: 2023-09-27 18:24:52

我有一个MVC项目,其中有一个对控制器的Json调用。这个呼唤没有任何回报,即使是你,它也应该总是回报真或假。

我的代码看起来像这个

注册.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 });
}

$.GetJson数据为空

在这种情况下从异步成功回调"返回"没有什么意义。在这种情况下,register函数似乎缺少了它自己的回调函数的一个额外参数——这并不是说"数据为空",而是说从未调用过适当的函数。

比较:

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');
};

请注意,上面的代码以一种愚蠢的方式使用了一个包装来显示一个观点。考虑一下它是否像下面这样编写,其中用户代码提供的回调在成功时仍然被调用。这是因为参数和函数上下文都不需要更改。

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

当然,统一的方法是使用Promises而不是手动回调包装。在这种情况下,因为$.get已经返回了一个promise,所以代码可以这样写:

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);
    });