$.ajax應用之請求頭headers


昨天項目中,由於要請求token驗證,后台給出的方案是采用請求頭返回token數據,給出的API文檔是這樣的

由於之前一直都是采用請求體發送請求,服務器在應答體李返回數據。和這個不一樣;

采用jq的$.ajax()函數發送請求,代碼如下

$.ajax({
            type: 'POST',
            url: '/token',
            headers:{"appId":appId,"appKey":appKey,"Content-Type":"text/plain;charset=UTF-8"},
            data:"{}",
            success: function(data, status, xhr){
               console.log(xhr);
            },
            error: function(xhr, type){
                window.location.reload();
            }

這樣寫正確,能夠正確發送請求,也能正常收到response headers:

使用jq的

getAllResponseHeaders()能得到所有response headers,取到的值是str類型;
使用
getResponseHeader('token')能得到token的值;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM