how to resolve this error “Use of getPreventDefault() is deprecated. Use defaultPrevented instead.”

为君一笑 提交于 2019-12-13 09:48:22

问题


I tried to fetch datas for a particualr user from sql sever database using json data. But its always showing error in the console as:

"Use of getPreventDefault() is deprecated. Use defaultPrevented instead."

And values are not retrieving from the database.

My code is :

Client-side:

<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>

Server-Side :

<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

But my datas are not displaying in the webpage. Thank you in advance


回答1:


This is just because you're using a version of jQuery that caters to older browsers in a slightly-sloppy fashion. The version you're using has this code in it:

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

As you can see, it does try to use the defaultPrevented property, but then goes on to call getPreventDefault if the default hasn't been prevented. So even on browsers with the property, if the default hasn't been prevented, it will call the old function if present.

Newer versions of jQuery (2.x, 3.x) don't use getPreventDefault anymore. If you want to get rid of the warning, either use a more up-to-date jQuery or hack the one you're using so that it checks for the existence of the property rather than just checking its value.



来源:https://stackoverflow.com/questions/41282673/how-to-resolve-this-error-use-of-getpreventdefault-is-deprecated-use-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!