weixin_33744854 2015-12-07 14:19 采纳率: 0%
浏览 14

其余删除中的Ajax错误

I tried to delete a record from database by calling a Rest service passing the Table id through ajax.

The function wired to click of delete button:

[$("#deleteRecord").click(function(){
        $.ajax({
        type:"DELETE",
        datatype:"text",
        url:"http:/localhost:8080/bookProject/book/bookAccess/deleteBook?bookNo="+$("#bookNo").val(),//URL of local rest service bookNo is the Id of input 
        success:function(data)
        {
            alert("success");
        },
        error:function(e)
        {
            alert("Ajax error");
            console.log(e);
        }
    });
});][1]

The server side code is as follows:

@DELETE
@Path("/deleteBook")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
    public String deleteBook(@QueryParam("bookNo")String bookNumber)
{
    String message="Deleted Succesfully";
// jdbc to delete from database 
  try
    {
        MysqlDataSource ds=new MysqlDataSource();
        ds.setServerName("localhost");
        ds.setPort(3306);
        ds.setUser("Sachu");
        ds.setPassword("Password01");
        Connection conn=ds.getConnection();
        Statement stmt=conn.createStatement();
        stmt.execute("delete from customer.booktable where bookNo="+bookNumber);
        conn.close();
    }
    catch(SQLException e)
    {
        message ="Error in deleting from the database";
    }
    return message;
}

// But when I click the delete button, the console displays a 404(Resource Not Found) Tomcat 6 error. I am able to do the operations PUT,POST and GET. Also when accessing from HTML document(not through localhost), DELETE does not throw an error. But when accessing through localhost(same origin),Tomcat throws an error. Can someone please let me know what the source of error is.Thanks in advance. Error Image

  • 写回答

1条回答 默认 最新

  • weixin_33712987 2015-12-16 07:57
    关注

    The answer was removing a part of the the url:"http:/localhost:8080/bookProject from the URL. It is enough to give relative path in the Rest project. With the url: book/bookAccess/deleteBook?bookNo="+$("#bookNo").val(), Deletion functionality is working fine.

    评论

报告相同问题?