{"id":4756,"date":"2022-08-20T18:25:28","date_gmt":"2022-08-20T10:25:28","guid":{"rendered":"https:\/\/wp.chunhsin.idv.tw\/?p=4756"},"modified":"2022-08-20T18:32:18","modified_gmt":"2022-08-20T10:32:18","slug":"%e4%bd%bf%e7%94%a8jquery-ajax%e8%b7%a8%e7%b6%b2%e5%9f%9fhttp-authyii2-cors%e8%a8%ad%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/wp.chunhsin.idv.tw\/?p=4756","title":{"rendered":"\u4f7f\u7528jQuery ajax\u8de8\u7db2\u57dfhttp auth+Yii2 cors\u8a2d\u5b9a"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">jQuery ajax\u8a2d\u5b9a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.ajax({\n    ....\n    xhrFields: {\n        withCredentials: true\n    },\n    ....\n});<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u662f\u8de8\u7db2\u57df\u7684ajax request\uff0c\u9019\u6bb5\u4e00\u5b9a\u8981\u52a0\uff0c\u4e0d\u7136\u5c31\u7b97server\u7aef\u7684cors\u6709\u8a2d\u597d\uff0c\u5230\u700f\u89bd\u5668\u9019\u88e1\u9084\u662f\u6703\u64cb\u6389http auth\u8a8d\u8b49\u3002\u540c\u7db2\u57df\u4e0b(\u540chttp\u5354\u5b9a\u3001\u7db2\u57df\u548cport)\u53ef\u7701\u7565\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wp.chunhsin.idv.tw\/wp-content\/uploads\/2022\/08\/1-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"298\" src=\"https:\/\/wp.chunhsin.idv.tw\/wp-content\/uploads\/2022\/08\/1-2.png\" alt=\"http auth\u7684\u5c0d\u8a71\u65b9\u584a\" class=\"wp-image-4758\"\/><\/a><figcaption>http auth\u7684\u5c0d\u8a71\u65b9\u584a<\/figcaption><\/figure>\n<\/div>\n\n\n<!--more-->\n\n\n\n<p class=\"has-medium-font-size\">Yii2 cors\u8a2d\u5b9a<\/p>\n\n\n\n<p>\u5728\u9700\u8a8d\u8b49\u7684controller\u7684behaviors\u52a0\u4e0a\u4e0b\u5217\u8a2d\u5b9a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public function behaviors()\n    {\n        return &#91;\n            ....\n            'cors' => &#91;\n                'class' => 'yii\\filters\\Cors',\n                'actions' => &#91;\n                    '{action-id}' => &#91;\n                        'Origin' => &#91;'http:\/\/localhost:8080'],\n                        'Access-Control-Allow-Credentials' => true,\n                     ],\n                ],\n            ],\n            ....\n        ];\n    }<\/code><\/pre>\n\n\n\n<p>\u6211\u9019\u908a\u662f\u63a1\u7528\u4f9daction id\u8986\u5bebcors\u8a2d\u5b9a\u7684\u65b9\u5f0f\u9032\u884c\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8986\u5beb yii\\filters\\Cors \u7684cors\u3002<\/p>\n\n\n\n<p>\u5728\u8de8\u7db2\u57df\u8981\u6c42http auth\u6642\uff0c\u700f\u89bd\u5668\u6703\u8981\u6c42\u56de\u61c9\u7684Access-Control-Allow-Origin\u6a19\u982d\u8981\u660e\u78ba\u6307\u5b9a\u7db2\u57df\uff0c\u4ee5\u5224\u65b7\u662f\u5426\u8207\u547c\u53eb\u7684\u7db2\u57df\u76f8\u7b26\uff0c\u82e5\u662f\u4f7f\u7528 *\u6703\u88ab\u700f\u89bd\u5668\u64cb\u6389\u3002\u5e95\u4e0b\u662fserver\u7aef\u6c92\u6709\u660e\u78ba\u6307\u5b9a Origin \u6642\uff0c\u700f\u89bd\u5668\u8f38\u51fa\u7684\u932f\u8aa4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-text-color\" style=\"color:#ff0d00\"><code>Access to XMLHttpRequest at 'https:\/\/php56.test\/cms2\/runYii.php?r=api\/authenticate\/login' from origin 'http:\/\/localhost:8080' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.<\/code><\/pre>\n\n\n\n<p>Access-Control-Allow-Credentials\u4e5f\u5fc5\u9808\u88ab\u6307\u5b9a\u5e03\u6797\u7d50\u679c\u70batrue\u7684\u503c\uff0c\u76f4\u63a5\u8a2d\u70batrue\u5c31\u53ef\u4ee5\u4e86\u3002\u5e95\u4e0b\u662f Access-Control-Allow-Credentials\u63a1\u9810\u8a2dnull\u6642\uff0c\u700f\u89bd\u5668\u8f38\u51fa\u7684\u932f\u8aa4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-text-color\" style=\"color:#ff0000\"><code>Access to XMLHttpRequest at 'https:\/\/php56.test\/cms2\/runYii.php?r=api\/authenticate\/login' from origin 'http:\/\/localhost:8080' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.<\/code><\/pre>\n\n\n\n<p>\u540c\u4e00\u500b\u7db2\u5740\u7684http auth request\u6703\u88ab\u700f\u89bd\u5668\u5feb\u53d6\u66ab\u5b58\uff0c\u5728\u95dc\u6389\u700f\u89bd\u5668\u4e4b\u524d\u4e00\u76f4\u90fd\u6703\u5b58\u5728\uff0c\u5982\u679c\u5df2\u7d93\u6210\u529f\u8a8d\u8b49\u904e\u4e00\u6b21\u7684\u8a71\uff0c\u5728\u6c92\u6709\u95dc\u9589\u700f\u89bd\u5668\u524d\u518d\u9032\u884chttp auth\uff0chttp auth\u5c0d\u8a71\u65b9\u584a\u5c31\u4e0d\u6703\u518d\u8df3\u51fa\uff0c\u6703\u4e00\u76f4\u6cbf\u7528\u9996\u6b21\u8a8d\u8b49\u6210\u529f\u7684\u5e33\u5bc6\uff0c\u5f88\u96e3\u5be6\u4f5c\u767b\u51fa\u7684\u884c\u70ba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery ajax\u8a2d\u5b9a \u5982\u679c\u662f\u8de8\u7db2\u57df\u7684ajax request\uff0c\u9019\u6bb5\u4e00\u5b9a\u8981\u52a0\uff0c\u4e0d\u7136\u5c31\u7b97server\u7aef\u7684co &hellip; <a href=\"https:\/\/wp.chunhsin.idv.tw\/?p=4756\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u4f7f\u7528jQuery ajax\u8de8\u7db2\u57dfhttp auth+Yii2 cors\u8a2d\u5b9a\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5,4,12],"tags":[405,410,349,411],"class_list":["post-4756","post","type-post","status-publish","format-standard","hentry","category-javascript","category-php","category-programming","category-freetalking","tag-cors","tag-http-auth","tag-yii2","tag-411"],"_links":{"self":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4756"}],"version-history":[{"count":6,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4756\/revisions"}],"predecessor-version":[{"id":4764,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4756\/revisions\/4764"}],"wp:attachment":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}