javascript - 如何在 REST 服务调用中包含相同 "action"的 HTML 表单 method=DELETE

标签 javascript html rest

我想在同一个 HTML 表单中包含 DELETE 方法,调用相同的操作(调用 REST 服务)。我该怎么做呢 ? 。我当前的 HTML 表单如下所示:DELETE 功能不起作用,但 POST 可以。我还包含了 REST 资源服务。如有任何建议,我们将不胜感激。

  <!DOCTYPE html>
<html>
 <head>
  <title>Form to create a new resource</title>
  <style>
    tab { padding-left: 4em; }
    </style>
 </head>
<body>

 <form id= "myForm" action="../com.dcr.jersey.first/rest/todos" >

  <label for="id">ID</label>
  <input name="id" />
  <br/>
  <label for="summary">Summary</label>
  <input name="summary" />
  <br/>
  Description:
  <TEXTAREA NAME="description" COLS=40 ROWS=6></TEXTAREA>
  <br/> 
  <button type="submit" value="Submit">Submit</button>  <tab> <button type="submit" value="Delete">Delete</button>
  <script>
      $("button.Submit").click(function(){
            $("#myForm").attr("method", "POST");
        });

        $("button.delete").click(function(){
            $("#myForm").attr("method", "DELETE");
        });
  </script>

</form>

</body>
</html> 

TodosResource.java

package com.dcr.jersey.jaxb.resources;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.dcr.jersey.jaxb.dao.TodoDao;
import com.dcr.jersey.jaxb.model.Todo;
//Will map the resource to the URL todos
@Path("/todos")
public class TodosResource {

    // Allows to insert contextual objects into the class,
      // e.g. ServletContext, Request, Response, UriInfo
      @Context
      UriInfo uriInfo;
      @Context
      Request request;
      @Context
      Todo todo;
    // Return the list of todos to the user in the browser
      @GET
      @Produces(MediaType.TEXT_XML)
      public List<Todo> getTodosBrowser() {
        List<Todo> todos = new ArrayList<Todo>();
        todos.addAll(TodoDao.instance.getModel().values());
        return todos;
      }
    // Return the list of todos for applications
      @GET
      @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
      public List<Todo> getTodos() {
        List<Todo> todos = new ArrayList<Todo>();
        todos.addAll(TodoDao.instance.getModel().values());
        return todos;
      }
    // retuns the number of todos
      // Use http://localhost:8080/de.vogella.jersey.todo/rest/todos/count
      // to get the total number of records
      @GET
      @Path("count")
      @Produces(MediaType.TEXT_PLAIN)
      public String getCount() {
        int count = TodoDao.instance.getModel().size();
        return String.valueOf(count);
      }

      @POST
      @Produces(MediaType.TEXT_HTML)
      @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
      public void newTodo(@FormParam("id") String id,
          @FormParam("summary") String summary,
          @FormParam("description") String description,
          @Context HttpServletResponse servletResponse) throws IOException {
        Todo todo = new Todo(id, summary);
        if (description != null) {
          todo.setDescription(description);
        }
        TodoDao.instance.getModel().put(id, todo);
        servletResponse.sendRedirect("../Created_PopUp.html");
      }  

      @POST
      @Produces(MediaType.TEXT_HTML)
      @Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})

      public void newTodoXMLJSON(Todo todo) throws IOException {
        this.todo=todo;
        TodoDao.instance.getModel().put(todo.getId(), todo);
      }  

      @DELETE
      @Produces(MediaType.TEXT_HTML)
      @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
      public void delTodo(@FormParam("id") String id,
          @Context HttpServletResponse servletResponse) throws IOException {
          Todo c = TodoDao.instance.getModel().remove(id);
            if(c==null)
              throw new RuntimeException("Delete: Todo with " + id +  " not found");
            else servletResponse.sendRedirect("../create_todo.html");
      }  

      @Path("{todo}")
      public TodoResource getTodo(@PathParam("todo") String id) {
            return new TodoResource(uriInfo, request, id);
      }
}

最佳答案

不是名为“formmethod”的属性,也不是方法。 method 属性通常位于表单级别。尝试 $("#myForm").attr("formmethod", "POST"); 相反

关于javascript - 如何在 REST 服务调用中包含相同 "action"的 HTML 表单 method=DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29944527/

相关文章:

javascript - Typescript 通用映射返回值

javascript - JQuery 绝对定位元素/不同的用户分辨率

javascript - 将 Svg 绘制到 Canvas ,包括嵌入在 svg 中的图像

rest - Maven 在编译时如何处理 WAR 依赖项?

Rest API,在参数中使用 '/'

javascript - 如何更改 Google Chart 中的 X 指标?

javascript - 在函数内调用 jQuery 函数

html - 阻止站点 Flash 播放器配置信息显示在 Google 上

python - 我想检查 html 文本作为条件,但是我应该如何处理 &nsbp 或 &#160?

c# - 尝试从 FitNesse REST URI 读取响应时出现连接关闭错误