使用JSON路由时出现问题

本文关键字:问题 JSON 路由 使用 | 更新日期: 2023-09-27 18:23:35

我正在使用JSON。但很明显,我的呼叫有问题,因为我没有进入AccountController中的GetJSON_Register。我不是Javascript方面的专家,但我认为这是我的脚本中的路由问题。

正如我所说,我对Javascript不是很好,所以我只想发布我的所有功能:

注册.cshtml

<form action="" method="post" role="form">
    <div class="form-group">
        <label for="email">Email</label>
        <input class="form-control" type="email" id="register-email" placeholder="Enter email" value="" required="required" />
    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input class="form-control" type="password" id="register-password" placeholder="Enter password" value="" required="required" />
    </div>
    <div class="form-group">
        <label for="repeatPassword">Repeat Password</label>
        <input class="form-control" type="password" id="register-repeatPassword" placeholder="Repeat password" value="" required="required" />
    </div>
    <hr />
    <div class="form-group">
        <label for="firstname">Firstname</label>
        <input class="form-control" type="text" id="register-firstname" placeholder="Enter firstname" value="" required="required" />
    </div>
    <div class="form-group">
        <label for="lastname">Lastname</label>
        <input class="form-control" type="text" id="register-lastname" placeholder="Enter lastname" value="" required="required" />
    </div>
    <button type="submit" class="btn btn-success" id="register-btn">Register</button>
    <div id="message-register">
        <span></span>
    </div>
</form>
<script type="text/javascript">
    $(document).ready(function () {
        $('#register-btn').click(function () {
            registera();
        });
    });
    function registera() {
        var $email = $('#register-email'),
            $password = $('#register-password'),
            $firstname = $('#register-firstname'),
            $lastname = $('#register-lastname'),
            $message = $('#message-register.span');
        $message.className = 'alert';
        showLoadingText($message);
        register($email, $password, $firstname, $lastname, function(data) {
            if (data.IsValid) {
                $message.html('');
                $message.className = '';
            } else {
                $message.className = 'alert alert-danger';
                $message.html(data.Message);
            }
        });
    };    
</script>

script.js

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

AccountController.cs

public ActionResult GetJSON_Register(string email, string password, string firstname, string lastname)
{
    string message = "";
    bool success = true;
    repository.Register(email, password, firstname, lastname);
    if(Error.Instance.Message != "")
    {
        message = Error.Instance.Message;
        success = false;
    }
    return Json(new { IsValid = success, Message = message }, JsonRequestBehavior.AllowGet);
}

编辑

尝试更改

<button type="submit" class="btn btn-success" id="register-btn">Register</button>

进入

<input type="button" class="btn btn-success" id="register-btn" onclick="registera()" value="Register" />

并移除CCD_ 6事件。

现在,在我的Chrome Javascript Console中,当点击按钮时,我会收到这条消息

 Uncaught InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('email') does not support selection. jquery-latest.min.js:4
     Vc jquery-latest.min.js:4
     Vc jquery-latest.min.js:4
     m.param jquery-latest.min.js:4
     m.extend.ajax jquery-latest.min.js:4
     m.(anonymous function) jquery-latest.min.js:4
     m.extend.getJSON jquery-latest.min.js:4
     register script.js:63
     registera Register:213
     onclick Register:180
GET http://localhost:1317/d61bfd81ee4f49eaae35adc38c5d9ea2/arterySignalR/ping?r…2Flocalhost%3A1317%2FAccount%2FRegister&browserName=Chrome&_=1411294396408 404 (Not Found) browserLink:37
    send browserLink:37
    p.extend.ajax browserLink:37
    i.transports._logic.pingServer browserLink:62
    (anonymous function) browserLink:62

使用JSON路由时出现问题

我找到了一个解决方案。我似乎正在将一个objectRegister.cshtml传递给script.js中的register函数,该函数接受value而不是object

因此,解决方案是更改Register.cshtml

register($email, $password, $firstname, $lastname, function(data) {
        if (data.IsValid) {
            $message.html('');
            $message.className = '';
        } else {
            $message.className = 'alert alert-danger';
            $message.html(data.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);
        }
    });