1. AJAX與傳統Form表單
實際上,兩者一般都是通過HTTP的POST請求。區別是浏覽器提交Form表單後,期望服務器返回一個完整的HTML頁面。而AJAX調用是由XMLHttpRequest對象(不同浏覽器可能不一樣)發出,浏覽器期望服務器返回HTML片段即可,具體是JSON、XML等都沒有要求。返回到浏覽器後如何使用,也是由JS腳本自己決定的。
2. 請求是不是AJAX
那麼對於服務器端,如何判斷一個HTTP請求是不是AJAX調用?這需要看HTTP的Header。
我們可以通過Header中的x-request-with來判斷。盡管不同浏覽器發送AJAX請求的對象不同,但是如果使用jQuery發送AJAX請求的話,jQuery內部實現ajax的時候,已經加入了標識。jQuery源碼中是這樣的:xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
所以,如果項目的前台頁面都是通過jQuery發送AJAX請求的話,這樣判斷是安全的。
下面是HTTP請求攜帶的Header信息。
普通Form表單提交
===MimeHeaders ===
accept = */*
referer =http://localhost:8080/user2/toQueryPage.action
accept-language = zh-CN
user-agent = Mozilla/4.0 (compatible; MSIE8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C;.NET4.0E)
accept-encoding = gzip, deflate
host = localhost:8080
connection = Keep-Alive
cache-control = no-cache
AJAX調用(IE)
===MimeHeaders ===
x-requested-with = XMLHttpRequest
accept-language = zh-cn
referer =http://localhost:8080/user2/toQueryPage.action
accept = application/json, text/javascript,*/*; q=0.01
content-type =application/x-www-form-urlencoded
accept-encoding = gzip, deflate
user-agent = Mozilla/4.0 (compatible; MSIE8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C;.NET4.0E)
host = localhost:8080
content-length = 57
connection = Keep-Alive
cache-control = no-cache
3. 在Action中獲得HTTP請求頭
在Action類中,通過ServletRequestAware接口獲得HttpServletRequest對象,再通過getHeader方法得到我們想要的頭信息。
public abstract class BaseAction
<ParamVo extends BaseParamVo, ResultVo extends BaseResultVo>
extends ActionSupport
implements ServletRequestAware {
private static final String AJAX_RESULT_NAME = "ajaxResult";
private static final String XHR_OBJECT_NAME = "XMLHttpRequest";
private static final String HEADER_REQUEST_WITH = "x-requested-with";
/**
* Request對象,用來判斷請求是否是AJAX調用
*/
private HttpServletRequest request;
private ParamVo paramVo;
private ResultVo resultVo;
@Override
public String execute() {
String resultPage = SUCCESS;
try {
resultVo = doExecute(paramVo);
}
catch (BaseException e) {
resultPage = ERROR;
}
if (XHR_OBJECT_NAME.equals(request.getHeader(HEADER_REQUEST_WITH))) {
resultPage = AJAX_RESULT_NAME;
}
return resultPage;
}
}