ajax成功函数出错

时间:2017-10-12 18:14:35

标签: java jquery ajax spring post

我正在创建一个简单的POST请求来从数据库中删除用户。数据库操作会通过,但在显示成功函数时会出错。谁能告诉我我的AJAX请求需要使用哪些正确的ajax头?我正在使用Tomcat 8.5和jquery 3.2.0。这是JS代码:

    $.ajax({
        url: "./users/remove",
        type: "POST",
        method: "POST",
        data : {
          "userId" : data.userId,
          "userName" : data.userName 
        },
        success : function(data)
        {
          alert(data);
        },
        error : function()
        {
          alert("There was an unexpected error when removing the users.");
        }
    });    

Java映射:

@RequestMapping(value = "/users/remove", method = RequestMethod.POST)
public String removeUser(@RequestParam(value="userId") String userId, @RequestParam(value="userName") String userName, HttpServletRequest request, HttpServletResponse response) 
{
RFQVBOImpl bo = new RFQVBOImpl();
String responseMsg = bo.removeUser(userId); 
String alertMsg;

if(responseMsg.equals("TRUE") || responseMsg.equals("DUPLICATE"))
{
alertMsg = userName + " was removed successfully.";
}
else 
{
alertMsg = "There was an error when removing " + userName;
}

return alertMsg;
}

编辑:我在这样的Javascript中不断收到通用的404错误。

POST http://localhost:8080/rfqv/users/add 404()

发送@jquery-3.2.0.min.js:4

ajax @ jquery-3.2.0.min.js:4

(匿名)@ view-users.js:107 //被调用函数的名称。

dispatch @ jquery-3.2.0.min.js:3

q.handle @jquery-3.2.0.min.js:3

我已将dataType:“text”添加到ajax标头并得到405错误: 请求方法'GET'不受支持

描述请求行中接收的方法由源服务器知道,但目标资源不支持。 *我将java方法与上面相同。

1 个答案:

答案 0 :(得分:1)

问题与你的标题无关。问题是您的处理程序返回String。如果您使用spring-mvc,则Spring默认情况下会认为当请求处理程序返回String时它是一个视图,因此它将尝试查找具有该名称的视图。

要让Spring知道您没有返回视图,您需要使用@ResponseBody注释:

@RequestMapping(value = "/users/remove", method = RequestMethod.POST, produces = "text/plain")
@ResponseBody
public String removeUser(@RequestParam(value="userId") String userId, @RequestParam(value="userName") String userName, HttpServletRequest request, HttpServletResponse response) 
{
  ...
}

您需要从ajax对象中删除type属性,现在应该使用dataType属性来预期文本响应:

$.ajax({
    url: "./users/remove",
    method: "POST",
    dataType: "text",
    data : {
      "userId" : data.userId,
      "userName" : data.userName 
    },
    success : function(data)
    {
      alert(data);
    },
    error : function()
    {
      alert("There was an unexpected error when removing the users.");
    }
});

这应该可行,但你真正应该做的是返回一个json对象。为了让你有一个想法:

public class RestResponse {
    private final boolean success;
    private final String message;

    public RestResponse(final boolean success, final String message)
    {
      this.success = success;
      this.message = message;
    }

    public boolean isSuccess()
    {
        return success;
    }

    public String getMessage()
    {
      return message,
    }
}

您的控制器仍然需要使用@ResponseBody返回上一个类的实例:

@RequestMapping(value = "/users/remove", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public RestReponse removeUser(@RequestParam(value="userId") String userId, @RequestParam(value="userName") String userName, HttpServletRequest request, HttpServletResponse response) 
{
  ...

  if(responseMsg.equals("TRUE") || responseMsg.equals("DUPLICATE"))
  {
    return new RestReponse(true, userName + " was removed successfully.");
  }

  return new Response(false, "There was an error when removing " + userName);
}

您的ajax对象现在需要json响应:

$.ajax({
    url: "./users/remove",
    method: "POST",
    dataType: "json",
    data : {
      "userId" : data.userId,
      "userName" : data.userName 
    },
    success : function(data)
    {
      alert(data.message);
    },
    error : function()
    {
      alert("There was an unexpected error when removing the users.");
    }
});

您可以扩展RestResponse类以创建ErrorRestResponse并添加字段以指定可能的错误。