在DELETE请求时参数为NULL

Parameter is NULL on DELETE Request

本文关键字:NULL 参数 DELETE 请求      更新时间:2023-09-26

我使用ajax发送删除请求,但我得到一个NULL值。我不知道我做错了什么。

这是我的ajax调用

(function() {
    $('#btnDelete').bind('click', function() {
      $.ajax({
        url: '/Project',
        type: 'DELETE',
        cache: false,
        dateType: 'json',
        data: {
          delProjId: $('#projectId').text(),
          nbRandom: Math.random().toString()
        },
        success: function(data) {
          if (data.message[0].error) {
            toastr.error(data.message[0].message);
          } else {
            toastr.success(data.message[0].message);
            console.log('success');
          }
        },
        error: function(request, status, error) {
          toastr.error(error);
        }
      });
    });

这是我的post request

protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setHeader("Content-Type", "application/json");
  String delProjId = request.getParameter("delProjId");
  System.out.println("Parm:" + delProjId);
}

和服务器日志

Parm:null
java.lang.NullPointerException
	at com.projectRequest.models.Project.doDelete(Project.java:122)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:210)

当前设置为span element。我尝试将值放在输入字段中,甚至在传递值时在值周围添加JSON.stringify()。我仍然得到相同的结果,一个空参数。我在客户端上看到这个值,甚至在执行ajax调用之前记录它。

只有POST请求在请求体中有参数作为实体。您必须将其作为URL的一部分发送。

参见RFC 2616:"DELETE方法请求源服务器删除由Request-URI标识的资源"。

您可以通过URI传递参数,例如:

http://localhost/?path=/willbedeleted.txt