`
wandejun1012
  • 浏览: 2687340 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

X-Requested-With

    博客分类:
  • ajax
 
阅读更多

 

ajax的请求和普通的http请求,在header里面多了个参数X-Requested-With

 

所以可以根据这个来区分。

 

 X-Requested-With请求头用于在服务器端判断request来自Ajax请求还是传统请求。

     两种请求在请求的Header不同,Ajax 异步请求比传统的同步请求多了一个头参数

  

       1  传统同步请求参数

    accept  text/html,application/xhtml+xml,application/xml;q=0.9,**
    accept-language  zh-cn
    referer  xxx

    x-requested-with  XMLHttpRequest
    content-type  application/x-www-form-urlencoded,text/javascript
    accept-encoding  gzip, deflate
    user-agent  Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; QQDownload 598
    host  192.168.101.72:8080
    content-length  233
    connection  Keep-Alive
    cache-control  no-cache
    cookie  CSS=undefined; JSESSIONID=1B9AC25036290F7FB6823CCE1A24E541

 

       可看到Ajax请求多了个x-requested-with,可以获取request.getHeader("X-Requested-With")
     
              bool isAjax = null;
              String header = request.getHeader("X-Requested-With");
              if(null != header && header.equals("XMLHttpRequest"))
             {
                       isAjax = true;
             }

           可以看到header为null则为传统同步请求。若为XMLHttpRequest则为Ajax请求。

 

 

refurl:http://woshixy.blog.51cto.com/5637578/976695

 

 

分享到:
评论

相关推荐

    如何从jQuery的ajax请求中删除X-Requested-With

    X-Requested-With常用于判断是不是ajax请求 但是有时我们会有需要删除X-Requested-With的情况 下面介绍一种方式 js代码 代码如下: $.ajax({ url: ‘http://www.zhangruifeng.com’, beforeSend: function( xhr ) { ...

    XMLHttpRequest对象

    XMLHttpRequest对象 深入理解 XMLHttpRequest对象

    nginx-1.15.10.zip

    add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; if ($request_method = 'OPTIONS') { return 204; } } }

    完美解决跨域问题和静态资源冲突的demo(png图片)

    response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Authorization,token"); // System.out.println("*********************************过滤器被使用************...

    tomcat解决跨域访问问题配置

    <param-value>token,Access-Control-Allow-Origin,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value> --> <param-value>Access-Control...

    java-property-utils-1.9.1等.zip

    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param...

    tomcat跨域文件下载和教程

    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param...

    tomcat跨域请求配置

    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-...

    实现跨域的JAR包

    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param...

    tomcat跨域访问支持Jar包

    < param-value > Accept, Origin, X-Requested-With, Content-Type, Last-Modified </ param-value > </ init-param > < init-param > < param-name > cors.exposedHeaders </ param-name > < param-value ...

    jx3box-data:数据json

    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; if ($request_method = 'OPTIONS') { ...

    redmine.js:基于 AngularJS 的 Redmine REST API 客户端

    Access-Control-Allow-Headers: X-Requested-With, X-Prototype-Version, Content-Type, X-Redmine-API-KeyAccess-Control-Allow-Methods: OPTIONS, GET, POST, PUT, DELETEAccess-Control-Allow-Origin: *您可以安

    PocCreator:GET和POST请求的POC创建者

    PocCreator GET和POST请求的POC创建者。 您只需要编辑脚本,然后选择输出文件夹,然后添加请求格式,例如Burp或Fiddler,例如本例: * ------------------------'''POST /... charset = UTF-8 X-Requested-With:X

    eBayjsonpipe.zip

    jsonpipe 是一个轻量级的 AJAX 客户端... "X-Requested-With": "XMLHttpRequest"  },  "data": "" // String. A serialized string to be sent in a POST/PUT request }); 标签:eBayjsonpipe

    待办事项客户

    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, access-control-allow-methods"); res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE...

    jRaiser-1.3.4.js / jRaiser-1.3.4-min.js.rar

    jRaiser 1.3.4 wath's new:  在$.util.parseTpl中添加容错处理,当模板为null时返回undefined,当values为空时返回模板内容。  优化了各种样式操作。... 在$.ajax.send的Ajax请求中增加X-Requested-With的header。

    php判断是否为ajax请求的方法

    jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以使用 $_SERVER[“HTTP_X_REQUESTED_WITH”] 来获取。(注意:中划线换成了下划线,不区分大小...

    yhsd-egg:友好速搭插件开发egg.js:egg:扩展框架——yhsd-egg:egg:!

    allowHeaders: 默认'Content-Type, Content-Length, Accept, X-Requested-With' allowMethods: 默认'PUT, POST, GET, DELETE, OPTIONS' } 插件 关闭CSRF防范,避免本地测试时报invalid csrf token错误 增加yhsd...

    play-cors:Play 的插件! 启用 CORS 的框架

    CORS 插件这个插件为 Scala Play 添加了支持! 框架应用程序。特征为对应用程序的所有请求提供飞行前 OPTIONS 响应。 提供一个 Play 过滤器,它将所..."Access-Control-Allow-Headers" -> "x-requested-with,content-ty

Global site tag (gtag.js) - Google Analytics