1. 程式人生 > 其它 >原生ajax與jquery ajax #請求頭的設定方法

原生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); } }
   }

演示的是相同的請求