下面的ajax方法在動作類和動作類失敗時觸發方法(所有棧跟蹤都帶有空指針異常);但我的阿賈克斯總是成功:功能。?請讓我知道爲什麼會發生這種情況。我應該在我的ajax調用中更改哪些內容?我需要提交嗎?我只是試圖從行動中獲取信息並獲得成功。 + 下面的ajax調用點擊不在IE中的操作類。在Firefox中運行良好。ajax沒有捕獲來自Struts2動作類的實際響應
這是一個行動類的方法。
public String pullData() {
try {
logger.debug("Pulling Data..!");
data.getInformation(); -> //throws null pointer exeption.
return SUCCESS;
} catch (Exception e) {
this.LOG.warn("Error while trying to execute quartz job: " + e);
e.printStackTrace();
return ERROR;
}
}
JSP
<input id="pullDataButton" class="button" type="button" value="Pull" />
AJAX方法
$("#pullDataButton").click(function() {
$.ajax({
traditional: true,
type: "POST",
url: "PullData.action",
async: true,
success: function(response) {
$('#placeHolder').html('Data Pulled!').css({color:'green'});
},
error: function(e) {
$('#placeHolder').html('Data Pull Failed!').css({color:'red'});
}
});
});
struts.xml的
<action name="pullData" method="pullData" class="com.pay.code.PullDataAction">
<result name="input">mainInfo.jsp</result>
<result name="error">mainInfo.jsp</result>
<result name="success">mainInfo.jsp</result>
</action>
有沒有辦法通過迴應或某種方式來追蹤「成功」和「錯誤」的行動返回?如果這不能完成,我必須使用JSON或param/hidden字段。我只需要知道在執行該方法時這是否成功或錯誤。 – user1769790
@ user1769790僅當您設置HTTP響應代碼時:如果S2返回正常結果,JS無法知道* S2 *認爲它是錯誤。 ''錯誤「'只是一個名字。您可以將錯誤/非錯誤信息編碼到JSP中,並跳過所有額外的工作,例如,如果存在狀態字段集,則JSP中會出現錯誤CSS。 –