2017-10-12 97 views
0

我正在創建一個簡單的POST請求,以從數據庫中刪除用戶。數據庫操作會經歷,但是在顯示成功函數時會出現錯誤。任何人都可以告訴我什麼是我需要用於我的AJAX請求的正確的ajax標頭?我正在使用Tomcat 8.5和jQuery 3.2.0。這裏是JS代碼:ajax成功函數中的錯誤

$.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 = "https://stackoverflow.com/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; 
} 

編輯:我一直得到一個通用的404錯誤在Javascript中是這樣的。

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 //被調用的函數名稱。

派遣@ jQuery的3.2.0.min.js:3

q.handle @ jQuery的3.2.0.min.js:3


我已經加入數據類型:「文本「到ajax頭部,並得到405錯誤: 不支持請求方法'GET'

說明請求行中收到的方法由原始服務器知道,但目標資源不支持。 *我離開java方法與上面相同。

+0

什麼是錯誤? –

+0

我在評論中添加了錯誤和額外信息。 – Leoking938

回答

1

問題不在於你的標題。問題是您的處理程序正在返回String。如果您使用的是spring-mvc,Spring默認情況下認爲請求處理程序返回一個字符串時它是一個視圖,因此它會嘗試查找具有該名稱的視圖。

爲了讓Spring知道你是不是返回一個視圖,你需要使用@ResponseBody註釋:

@RequestMapping(value = "https://stackoverflow.com/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 = "https://stackoverflow.com/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並添加一個字段中指定possibl e錯誤。

+0

謝謝!這解釋了爲什麼我的用戶/所有方法都在工作,但其他人卻沒有。感謝您提供有關返回JSON作爲迴應的提示,對春天來說還是比較新的。 – Leoking938