如何解决此错误"不推荐使用getPreventDefault()。请改用defaultPrevented。"

时间:2016-12-22 11:57:06

标签: javascript jquery json vb.net sql-server-2005-express

我尝试使用json数据从sql sever数据库获取特定用户的数据。但它始终在控制台中显示错误:

  

"不推荐使用getPreventDefault()。使用defaultPrevented   代替"

并且不会从数据库中检索值。

我的代码是:

客户端:

<body>
<form id="form1" runat="server">
<table border="0" >
    <tr>
        <td>
            <asp:Label ID= "lblName" runat="server" Text="Name" ></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server" Text="" /><br />
        </td>
    </tr>
    <tr>
            <td> &nbsp </td>
    </tr>
    <tr>
        <td colspan ="2" >
           <asp:Button ID="btnShow" Text="Show" runat="server" />
        </td>
    </tr>
</table>
    <hr /><asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2"
    HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2">
    <Columns>
        <asp:BoundField DataField="Username" HeaderText="Username" />
        <asp:BoundField DataField="Password" HeaderText="Password" />
    </Columns>
</asp:GridView> 
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnShow]").bind("click", function () {
            var user = {};
            user.Name = $("[id*=txtName]").val();
            user.grd = $("[id*=gvUsers]").val();
            $.ajax({
                type: "POST",
                url: "View.aspx/ViewUser",
                data: '{user: ' + JSON.stringify(user) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    //alert("User has been added successfully.");
                    window.location.reload();
                }
            });
            return false;
        });
    });
</script>
</body>

服务器端:

<WebMethod()> _
<ScriptMethod()> _
Public Shared Sub ViewUser(user As Users)
    Dim grd As GridView
    grd = user.grd
    'Dim gvUsers As GridView
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT * FROM Users Where Username = @Name")
            Using sda As New SqlDataAdapter()
                Dim dt As New DataTable()
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Name", user.Name)
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
                'grd.Visible = True
                grd.DataSource = dt
                grd.DataBind()
            End Using
        End Using
    End Using
End Sub
End Class
Public Class Users
Public Property Name() As String
    Get
        Return _Name
    End Get
    Set(value As String)
        _Name = value
    End Set
End Property
Private _Name As String
Public Property grd() As GridView
    Get
        Return _grd
    End Get
    Set(value As GridView)
        _grd = value
    End Set
End Property
Private _grd As GridView
End Class

但是我的数据没有显示在网页上。提前谢谢

1 个答案:

答案 0 :(得分:1)

这只是因为你正在使用一种jQuery版本,以一种略显草率的方式迎合旧版浏览器。您正在使用的版本中包含以下代码:

this.isDefaultPrevented=e.defaultPrevented||e.returnValue===!1||e.getPreventDefault&&e.getPreventDefault()

如您所见,它确实尝试使用defaultPrevented属性,但如果未阻止默认值,则继续调用getPreventDefault。所以即使在具有该属性的浏览器上,如果没有阻止默认值,它也将调用旧函数(如果存在)。

较新版本的jQuery(2.x,3.x)不再使用getPreventDefault。如果你想摆脱警告,要么使用更新的jQuery,要么破解你正在使用的jQuery,以便它检查属性是否存在而不仅仅是检查它的值。