Ajax请求带token,返回静态页面,兼容ie的方法

请先确保另外一个页面的head与该页面的head一致,才能支持ie版本小于10的浏览器,因为此时head是只读的,只有ie版本大于9,head才能在当前页变动,所以:


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Ajax</title>
  <link rel="stylesheet" href="./static/css/App.css">
</head>
function ajax(){
    $.ajax({
      url: "http://localhost:3000/admin/index.html",
      type: "GET",
      dataType: "html",
      headers: {
        authorization: document.getElementById("token").value
      },
      async: false,
      success: function(data) {//高版本ie的浏览器,允许直接全部替换
        if(document.documentMode >9|| document.documentMode == undefined){
          var a=document.createElement("html");a.innerHTML=data.replace(/(<!|<)\w* ?html>\n?/g,"")
          document.documentElement.parentNode.replaceChild(a,document.documentElement)
        }else{//当在低版本ie的情况下,可以直接替换body来实现
          data=data.replace(/\n?(<!|<)\w* ?html ?(lang="en")?>|<\/html>/g,"")
          document.body.innerHTML=data.replace(/.*(?=)+<body/g,"<body")
          // document.head.innerText=data.replace(/\<head\>((\n.*)+)\<\/head\>/g,"$1")
          // document.head在ie<10版本会是只读,所以若兼容ie10以下的版本,head尽量保持一致
        }
      },
      error: function(data) {
        document.getElementById("ajax").innerText=data.statusText
        // console.log(data);
      }
    });
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值