$ .GetJson数据为空

时间:2014-09-27 21:34:27

标签: c# jquery asp.net-mvc json

我正在进行MVC项目,我对控制器进行Json调用。这个调用什么都不返回,即使它总是应该返回true或false。

我的代码看起来像这样

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

1 个答案:

答案 0 :(得分:1)

在此上下文中从异步成功回调中“返回”没有意义。在这种情况下,看起来寄存器函数缺少它自己的回调函数的附加参数 - 它不是“数据是空的”,因为从不调用适当的函数。

与:比较:

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

当然,统一的方法是use 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);
    });