2016-07-28 94 views
0

我有這個Ajax調用工作正常。控制器接收到該呼叫並執行它應該做的事情。如何訪問百里香葉中的ajax請求參數

控制器方法(在這裏我故意拋出異常測試,我需要顯示錯誤消息)

@RequestMapping(value = "/deleteContentFromMarineCustomer", method = RequestMethod.DELETE, produces = "application/json") 
public HttpEntity<String> deleteContentFromMarineCustomer(@RequestParam(value = "srcId") String srcId, @RequestParam(value = "customerId") String customerId, @RequestParam(value = "locale") String locale, @RequestParam(value = "method") String method) { 
    ResponseEntity<String> responseEntity = null; 
    try { 
     // oneChartService.deleteContentFromMarineCustomer(customerId, srcId); 
     // responseEntity = new ResponseEntity<String>(HttpStatus.OK); 
     throw new Exception("test"); 
    } catch (Exception e) { 
     responseEntity = new ResponseEntity<String>("Delete", HttpStatus.SERVICE_UNAVAILABLE); 
    } 

    return responseEntity; 

} 

現在我的JS是這樣的

function deleteSelectedContent() { 
var link = "content/deleteContentFromMarineCustomer?srcId=" + srcId + "&customerId=" + customerId + "&locale=" + locale+"&method=Delete"; 
jQuery.ajax({ 
    url : link, 
    type : 'DELETE', 
    success : function(data, status) { 
     window.location.reload(); 
    }, 
    error : function(data, status) { 
     jQuery("#errorContentModal").show(); 
    } 
}); 

現在顯示在我的問題是我想將方法​​名稱傳遞給它的頁面,以便它可以顯示正確的消息

我的html

<div id="errorContentModal" class="modal"> 
    <div class="modal-dialog-content" align="center"> 
    <h3 th:text="#{serviceContentError(${#httpServletRequest.getParameter('method')})}"></h3> 
    <!--<h3 th:text="#{serviceContentError('Delete')}"></h3>--> 
    <button class="close" th:text="#{button.ok}"></button> 
    </div> 
</div> 

基本上,註釋掉的行有硬編碼的文本,我想從請求中讀取,這樣我可以使此代碼通用於其他方法使用。

問題是#httpServletRequest.getParameter('method')返回null。我原來的網頁調用locale = en_US,所以當我這樣做#httpServletRequest.getParameter('locale')這工作正常。由於這是一個Ajax調用,出於某種原因,它不理解它。

有誰知道爲什麼我不能讀這個代碼的Ajax請求調用?

回答

0

你可以試試以下,讓我知道這是否適合你。

錯誤回調中的數據對象將有返回消息,爲什麼不在那裏設置錯誤消息。

error : function(data, status) { 
     //test if data object return the correct value 
     console.log(data); 
     console.log(data.delete); 
     $("#test").val(data.delete); 
     jQuery("#errorContentModal").show(); 
} 

和在html聲明ID(TEST3)

<h3 id="test"></h3>