简体   繁体   English

未捕获的TypeError:非法调用

[英]Uncaught TypeError: Illegal invocation

I am trying to delete users from my database using Ajax, Servlet and HTML. 我正在尝试使用Ajax,Servlet和HTML从数据库中删除用户。 when I submit data Illegal Invocation occurs. 当我提交数据时,发生非法调用。 I think that there won't be any problems with connection or SQL statement 我认为连接或SQL语句不会有任何问题

delete.html delete.html

<input type="text" id="delete">
<input type="submit" onclick="deleteUSer()" value="Delete">

delete.js delete.js

function deleteUSer(){

        var username = document.getElementById("delete");

        var params = {

            username: username
        }

    $.post("Delete", params, function(data){}
            )

}

Delete.java servlet Delete.java servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = null;
        DBUtils dbutils = null;
        Connection conn = null;
        try{

            String username = request.getParameter("username");

            dbutils = new DerbyUtils();
            conn = dbutils.getConnection();

            DeleteDAO dao = new DeleteDAO(conn);

            dao.deleteUser(username);



            RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher("/index.html");
            dispatcher.forward(request, response);



        }

DeleteDAO.java DeleteDAO.java

public void deleteUser(String username) throws SQLException{
      try{


          String sql = "delete from users where username='"+username+"'";

          PreparedStatement ps = this.conn.prepareStatement(sql);

          ps.executeQuery();

you are sending html doc elemect not username ... 您正在发送html doc elemect而不是用户名...

try this -- 尝试这个 -

<input type="text" id="delete" value="vivek">
<input type="submit" onclick="deleteUSer()" value="Delete">


    function deleteUSer(){

            var username = document.getElementById("delete");

            var params = {

                username: username.value
            }

        $.post("Delete", params, function(data){}
                )

    } 

If you're trying to enact the "delete" command that you can use with HTTP requests, you're going to want to use $.ajax , not $.post . 如果您试图制定可用于HTTP请求的“删除”命令,则将要使用$.ajax而不是$.post

To quote the docs ( https://api.jquery.com/jQuery.post/ ): 引用文档( https://api.jquery.com/jQuery.post/ ):

This is a shorthand Ajax function, which is equivalent to: 这是Ajax的简写功能,等效于:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

if you're trying to enact the "delete", you'd want to do something like: 如果您要执行“删除”操作,则需要执行以下操作:

var dataObj = {
        'username': document.getElementById("delete").value;
    }
$.ajax({
    type:"DELETE",
    data: dataObj,
    url: 'url/to/your/servlet'
});

You may also want to include a datatype in there but for this example jquery can figure it out. 您可能还希望在其中包含一个数据类型,但是对于本示例而言,jquery可以找出它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM