<bdo id='LetL1'></bdo><ul id='LetL1'></ul>

<small id='LetL1'></small><noframes id='LetL1'>

    <i id='LetL1'><tr id='LetL1'><dt id='LetL1'><q id='LetL1'><span id='LetL1'><b id='LetL1'><form id='LetL1'><ins id='LetL1'></ins><ul id='LetL1'></ul><sub id='LetL1'></sub></form><legend id='LetL1'></legend><bdo id='LetL1'><pre id='LetL1'><center id='LetL1'></center></pre></bdo></b><th id='LetL1'></th></span></q></dt></tr></i><div id='LetL1'><tfoot id='LetL1'></tfoot><dl id='LetL1'><fieldset id='LetL1'></fieldset></dl></div>
    <tfoot id='LetL1'></tfoot>
    1. <legend id='LetL1'><style id='LetL1'><dir id='LetL1'><q id='LetL1'></q></dir></style></legend>

      CORS 请求 - 为什么不发送 cookie?

      时间:2023-09-03
    2. <small id='OJjqt'></small><noframes id='OJjqt'>

          <bdo id='OJjqt'></bdo><ul id='OJjqt'></ul>

            <i id='OJjqt'><tr id='OJjqt'><dt id='OJjqt'><q id='OJjqt'><span id='OJjqt'><b id='OJjqt'><form id='OJjqt'><ins id='OJjqt'></ins><ul id='OJjqt'></ul><sub id='OJjqt'></sub></form><legend id='OJjqt'></legend><bdo id='OJjqt'><pre id='OJjqt'><center id='OJjqt'></center></pre></bdo></b><th id='OJjqt'></th></span></q></dt></tr></i><div id='OJjqt'><tfoot id='OJjqt'></tfoot><dl id='OJjqt'><fieldset id='OJjqt'></fieldset></dl></div>

            1. <legend id='OJjqt'><style id='OJjqt'><dir id='OJjqt'><q id='OJjqt'></q></dir></style></legend>

              <tfoot id='OJjqt'></tfoot>
                <tbody id='OJjqt'></tbody>
                本文介绍了CORS 请求 - 为什么不发送 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个成功预飞行的跨域 AJAX GET,但 cookie 没有附加到 GET 请求.当用户单击登录按钮时,会进行 POST 以使用户登录,这可以跨域正常工作.JavaScript 是:

                I have a cross-domain AJAX GET which gets pre-flighted successfully, but the cookies don't get attached to the GET request. When the user clicks a log in button, a POST is made to log the user in, which works correctly cross domain. The JavaScript is:

                        $.ajax(signin_url, {
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(credentials),
                            success: function(data, status, xhr) {
                                signInSuccess();
                            },
                            error: function(xhr, status, error) {
                                signInFailure();
                            },
                            beforeSend: function(xhr) {
                                xhr.withCredentials = true
                            }
                        });
                

                响应标头包含一个 cookie:

                The response headers include a cookie:

                Set-Cookie:user_token=snippysnipsnip; path=/; expires=Wed, 14-Jan-2032 16:16:49 GMT
                

                如果登录成功,则发出 JavaScript GET 请求以获取当前用户的详细信息:

                If sign-in succeeds, a JavaScript GET request is made to get the current user's details:

                function signInSuccess() {
                    $.ajax(current_user_url, {
                        type: "GET",
                        contentType: "application/json; charset=utf-8",
                        success: function(data, status, xhr) {
                            displayWelcomeMessage();
                        },
                        beforeSend: function(xhr) {
                            xhr.withCredentials = true;
                        }
                    });
                }
                

                Chrome 的 OPTIONS 请求返回的与 CORS 相关的标头是:

                The CORS-related headers returned from Chrome's OPTIONS request are:

                Access-Control-Allow-Credentials:true
                Access-Control-Allow-Headers:X-Requested-With, X-Prototype-Version, Content-Type, Origin, Allow
                Access-Control-Allow-Methods:POST, GET, OPTIONS
                Access-Control-Allow-Origin:http://192.168.0.5
                Access-Control-Max-Age:1728000
                

                但是,GET 请求不会发送任何 cookie.

                However, no cookies are sent on the GET request.

                推荐答案

                问题出在 jQuery 调用上 - 从 1.5 开始似乎应该将 withCredentials 指定为:

                The issue was with the jQuery calls - it seems since 1.5 withCredentials should be specified as:

                        $.ajax("http://localhost:3000/users/current", {
                            type: "GET",
                            contentType: "application/json; charset=utf-8",
                            success: function(data, status, xhr) {
                                hideAllContent();
                                $("#sign_out_menu_item").show();
                                $("#sign_in_menu_item").hide();
                                $("#welcome").text("Welcome " + data["username"] + "!");
                                $("#welcome").show();
                            },
                            xhrFields: {
                                withCredentials: true
                            },
                            crossDomain: true
                        });
                

                这篇关于CORS 请求 - 为什么不发送 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在任何地方使用 Cors 反向代理和添加 CORS 标 下一篇:跨域资源共享 (CORS) 概念

                相关文章

                最新文章

                    <legend id='2JxN5'><style id='2JxN5'><dir id='2JxN5'><q id='2JxN5'></q></dir></style></legend>

                    <small id='2JxN5'></small><noframes id='2JxN5'>

                  1. <i id='2JxN5'><tr id='2JxN5'><dt id='2JxN5'><q id='2JxN5'><span id='2JxN5'><b id='2JxN5'><form id='2JxN5'><ins id='2JxN5'></ins><ul id='2JxN5'></ul><sub id='2JxN5'></sub></form><legend id='2JxN5'></legend><bdo id='2JxN5'><pre id='2JxN5'><center id='2JxN5'></center></pre></bdo></b><th id='2JxN5'></th></span></q></dt></tr></i><div id='2JxN5'><tfoot id='2JxN5'></tfoot><dl id='2JxN5'><fieldset id='2JxN5'></fieldset></dl></div>

                      <bdo id='2JxN5'></bdo><ul id='2JxN5'></ul>
                    <tfoot id='2JxN5'></tfoot>