2011-12-29 14 views
5

對於Spring MVC控制器中的驗證錯誤,我將自定義標頭錯誤設置爲響應標題。我可以在Firefox 3.5中訪問該響應頭。但不是在IE 8中。 請告訴我,將自定義錯誤消息顯示給Jquery客戶端的正確方法。getResponseHeader在IE 8中返回null

var jqxhr=$.post("saveAcc.htm",{ data: data}); 

    jqxhr.success(function() { 
     alert("Saved"); 
    }); 

    jqxhr.error(function(thrownError){ 
     fnSetError(jqxhr.getResponseHeader('error')); 
     alert(jqxhr.getAllResponseHeaders()); //returns empty 
     //alert('responseText '+ thrownError.responseText); 
     alert(jqxhr.getResponseHeader('error')); //return null      
     oTable.fnReloadAjax(); 
    }); 
+0

我不知道這是否會有所幫助,但試試這個:http://forum.jquery.com/topic/jquery-ajax-ie8-problem,這一個關於IE8中jquery $ .ajax的bug報告,以及一些可能的修復。 – joao 2013-09-13 13:58:52

回答

0

如果要調用JS錯誤處理函數,請在服務器上引發異常。

或者,你可以設置的HTTPStatus 500或一些其他錯誤狀態

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
//response.setStatus(500); 

但我認爲你的做法是錯誤的。驗證錯誤不是應用程序錯誤,您應該處理成功消息中的錯誤。

jqxhr.success(function(data) { 
    if(data['validationMessages'].length > 0) { 
     //deal with the validation issues 
    } 
}); 

這可能會幫助:http://outbottle.com/spring-3-web-mvc-exception-handling-incorporating-ajax/