1

大家好,我使用以下代码检查数据是否存在

这是我的输入设计

<label>
 @Html.LabelFor(u => u.Username)</label>
<input type="text" id="username" />
 &nbsp;
<label id="UserNameAvailabilityLabel" class="input_text">
</label>
 <span class="input_error">
 @Html.ValidationMessageFor(u => u.Username)
</span>
</label>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var emptyUserNameMessage = 'Please enter the username';
var progressUserNameMessage = 'Checking...';
var availableUserNameMessage = 'Username is available';
var usedUserNameMessage = 'Username has been taken';


$(function () {
    var userNameAvailabilityLabel = $("#UserNameAvailabilityLabel");

    $("#username").blur(function () {
        var userNameTextBox = $("#username");
        var userName = userNameTextBox.val();
        if ($.trim(userName) == '') {
            userNameAvailabilityLabel
                        .removeClass()
                        .addClass('required1')
                        .html(emptyUserNameMessage);
        }
        else {
            userNameAvailabilityLabel.html('');
            $("#ProgressDiv").show();

            $.ajax({
                type: 'POST',
                url: 'Checkavailability.asmx/CheckUserNameAvailability',
                data: '{userName: \'' + userNameTextBox.val() + '\'}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: OnCheckUserNameAvailabilitySuccess,
                error: OnCheckUserNameAvailabilityError
            });
        }
        return false; //Prevent postback
    });

    function OnCheckUserNameAvailabilitySuccess(response) {
        $("#ProgressDiv").hide();
        if (response != null && response.d != null) {
            var data = response.d;
            switch (data) {
                case 0:
                    userNameAvailabilityLabel
                        .removeClass()
                        .addClass('available')
                        .html(availableUserNameMessage);
                    $("#btnSubmit").removeAttr('disabled');
                    break;
                case 1:
                    userNameAvailabilityLabel
                        .removeClass()
                        .addClass('used')
                        .html(usedUserNameMessage);
                    $("#btnSubmit").attr('disabled', 'disabled');
                    break;
            }
        }
    }
    function OnCheckUserNameAvailabilityError(xhr, ajaxOptions, thrownError) {
        alert(xhr.statusText);
    }
});

这在我在普通aspx页面中使用它时有效,但是当MVC执行第一个脚本时,这意味着如果我从文本框移动而没有输入数据,它会根据需要显示错误,如果输入一些文本并离开文本框弹出一个内部服务器错误可能是什么问题

4

1 回答 1

1

url: 'Checkavailability.asmx/CheckUserNameAvailability',刚改成url: "../Checkavailability.asmx/CheckUserNameAvailability",

于 2013-02-05T07:16:10.780 回答