CORS是ajax跨域請求的一個解決方案,詳細見:css
瀏覽器同源政策及其規避方法
跨域資源共享 CORS 詳解html
ajax能夠設置請求頭,和get響應頭。jquery
jQuery and AJAX response header
How can I add a custom HTTP header to ajax request with js or jQuery?git
先感覺一下,編寫index.html:github
<!DOCTYPE html> <html> <head> <title>test</title> </head> <body> <button id="test">click</button> <script src="//cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script> <script> $('#test').on('click', function(){ console.log('click me'); var url = 'https://api.github.com/users/octocat/gists'; var geturl = $.ajax({ type: "GET", url: 'https://api.github.com/users/octocat/gists', success: function (data) { console.log('header:\n', geturl.getAllResponseHeaders()); console.log('data:\n', data); } }); }); </script> </body> </html>
npm安裝http-server模塊,在index.html目錄下執行http-server命令,瀏覽器打開 http://127.0.0.1:8080/
,點擊按鈕,看下console的輸出:ajax
這個響應頭是被過濾過的,另外原則上是沒法獲取Set-cookie的值。 見How to get a cookie from an AJAX response? 。npm
jQuery如何獲取響應頭: http://api.jquery.com/jQuery.ajax/#jqXHRapi