ajax請求與傳統http請求區分

關鍵是看http請求頭(header)信息中的 x-requested-with 信息項是否有無,若是無該頭信息項,取出的是未定義的值,即null,則說明是傳統請求;若是存在該頭信息項,值是XMLHttpRequest 則說明是Ajax請求; 異步

 

JAVA WEB項目,好比在Server端的JSP中判斷以下: spa

if (request.getHeader("x-requested-with") != null   get

    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {   同步

    out.print("該請求是 AJAX 異步HTTP請求。");   it

}else{    request

    out.print("該請求是傳統的 同步HTTP請求。");   請求

}   項目

相關文章
相關標籤/搜索