CORS、Ajax和Cookie

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

相关文章
相关标签/搜索