原生ajax与jquery ajax #请求头的设置方法

        $.ajax({
            type:"get",
            url: "http://localhost/user/test",
            headers: {
              "Authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzMSJdLCJ1c2VyX25hbWUiOiJ7XCJlbWFpbFwiOlwiMjExOTI5OTUzMUBxcS5jb21cIixcImZ1bGxuYW1lXCI6XCLlsI_luoRcIixcImlkXCI6MyxcInBhc3N3b3JkXCI6XCIkMmEkMTAkc1o4d0d4NHkvcG1CM1g4QkFGZWxTLmpqR2M2TE8weDluMWFzR1cvWEVPT2VQMGF1dmFkSnFcIixcInVzZXJuYW1lXCI6XCIxODQ3NjMwNTY0MFwifSIsInNjb3BlIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiLCJST0xFX0FQSSJdLCJleHAiOjE2MjM4Mzc4MDUsImF1dGhvcml0aWVzIjpbImJhc2UiXSwianRpIjoiMWRiOTIxYjUtNGZjMS00NWZkLThmNDAtZTA0NTBkZTMyODEyIiwiY2xpZW50X2lkIjoiYzEifQ.-u2Zzn9iWtb15SSijx3kccxBJyJr6_56ktkG3gVrajM"
            },
            success(resource) {
              console.log(resource);
            },
            error(err) {
              console.log(err);
            }

        })
        
       
      })

   //原生 window.onload
=function() { var xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }else { xmlHttp = window.ActiveXObject("Microsoft.XMLHTTP"); } //数据组装 var data = {"Authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzMSJdLCJ1c2VyX25hbWUiOiJ7XCJlbWFpbFwiOlwiMjExOTI5OTUzMUBxcS5jb21cIixcImZ1bGxuYW1lXCI6XCLlsI_luoRcIixcImlkXCI6MyxcInBhc3N3b3JkXCI6XCIkMmEkMTAkc1o4d0d4NHkvcG1CM1g4QkFGZWxTLmpqR2M2TE8weDluMWFzR1cvWEVPT2VQMGF1dmFkSnFcIixcInVzZXJuYW1lXCI6XCIxODQ3NjMwNTY0MFwifSIsInNjb3BlIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiLCJST0xFX0FQSSJdLCJleHAiOjE2MjM4Mzc4MDUsImF1dGhvcml0aWVzIjpbImJhc2UiXSwianRpIjoiMWRiOTIxYjUtNGZjMS00NWZkLThmNDAtZTA0NTBkZTMyODEyIiwiY2xpZW50X2lkIjoiYzEifQ.-u2Zzn9iWtb15SSijx3kccxBJyJr6_56ktkG3gVrajM"} //发送请求 xmlHttp.open("get","http://localhost/user/test",true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzMSJdLCJ1c2VyX25hbWUiOiJ7XCJlbWFpbFwiOlwiMjExOTI5OTUzMUBxcS5jb21cIixcImZ1bGxuYW1lXCI6XCLlsI_luoRcIixcImlkXCI6MyxcInBhc3N3b3JkXCI6XCIkMmEkMTAkc1o4d0d4NHkvcG1CM1g4QkFGZWxTLmpqR2M2TE8weDluMWFzR1cvWEVPT2VQMGF1dmFkSnFcIixcInVzZXJuYW1lXCI6XCIxODQ3NjMwNTY0MFwifSIsInNjb3BlIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiLCJST0xFX0FQSSJdLCJleHAiOjE2MjM4Mzc4MDUsImF1dGhvcml0aWVzIjpbImJhc2UiXSwianRpIjoiMWRiOTIxYjUtNGZjMS00NWZkLThmNDAtZTA0NTBkZTMyODEyIiwiY2xpZW50X2lkIjoiYzEifQ.-u2Zzn9iWtb15SSijx3kccxBJyJr6_56ktkG3gVrajM"); //发送数据 xmlHttp.send(data); //事件监听 xmlHttp.onreadystatechange=function () { //状态信息 if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { var result = xmlHttp.responseText; console.log(result); } }
   }

演示的是相同的请求

posted @ 2021-06-16 17:36  小庄的blog  阅读(557)  评论(0编辑  收藏  举报