码迷,mamicode.com
首页 > Web开发 > 详细

如何判断一个请求为ajax请求?

时间:2018-02-01 10:36:47      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:str   return   如何   tty   out   headers   请求头   image   username   

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和XML)。

ajax的请求头如下:

技术分享图片

如上图所示具有“X-Request-With”属性,该属性的值为“XMLHttpRequest”[AJAX请求中主要对象(原生对象)]

而普通请求的请求头为:

技术分享图片

示例代码如下:

String requestType = request.getHeader("X-Requested-With");
if("XMLHttpRequest".equals(requestType)){
    System.out.println("AJAX请求..");
}else{
    System.out.println("非AJAX请求..");
    //此时requestType为null
}

同样,可以根据此属性来限制方法只能接受AJAX请求。

 @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "X-Requested-With=XMLHttpRequest" })
    public String testParamsAndHeaders() {
        System.out.println("testParamsAndHeaders");
        return SUCCESS;
    }

 

如何判断一个请求为ajax请求?

标签:str   return   如何   tty   out   headers   请求头   image   username   

原文地址:https://www.cnblogs.com/lxcmyf/p/8397430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!