{"id":14,"date":"2022-04-04T15:31:45","date_gmt":"2022-04-04T07:31:45","guid":{"rendered":"https:\/\/reverieland.cn\/?p=14"},"modified":"2022-04-04T18:27:20","modified_gmt":"2022-04-04T10:27:20","slug":"ajax%e8%b7%a8%e5%9f%9f%e8%ae%bf%e9%97%aecookie%e4%b8%a2%e5%a4%b1%e9%97%ae%e9%a2%98%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/reverieland.cn\/index.php\/14\/","title":{"rendered":"Ajax\u8de8\u57df\u8bbf\u95eeCookie\u4e22\u5931\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1.ajax\u8de8\u57df\u8bbf\u95ee,cookie\u4e22\u5931<\/h3>\n\n\n\n<p>\u521b\u5efa\u4e24\u4e2a\u6d4b\u8bd5\u57df\u540d\uff1aa.fdipzone.com \u4f5c\u4e3a\u5ba2\u6237\u7aef\u57df\u540d\uff0cb.fdipzone.com \u4f5c\u4e3a\u670d\u52a1\u7aef\u57df\u540d\uff0c\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/setcookie.php \u7528\u4e8e\u8bbe\u7f6e\u670d\u52a1\u7aefcookie\n&lt;?php\nsetcookie('data', time(), time()+3600);\n?&gt;\n\/\/server.php \u7528\u4e8e\u88ab\u5ba2\u6237\u7aef\u8bf7\u6c42\n&lt;?php\n$name = isset($_POST&#91;'name'])? $_POST&#91;'name'] : '';\n$ret = array( 'success' =&gt; true, 'name' =&gt; $name,\n'cookie' =&gt; isset($_COOKIE&#91;'data'])? $_COOKIE&#91;'data'] : ''\n);\n\/\/ \u6307\u5b9a\u5141\u8bb8\u5176\u4ed6\u57df\u540d\u8bbf\u95ee\nheader('Access-Control-Allow-Origin:http:\/\/a.fdipzone.com');\n\/\/ \u54cd\u5e94\u7c7b\u578b\nheader('Access-Control-Allow-Methods:POST');\n\/\/ \u54cd\u5e94\u5934\u8bbe\u7f6e\nheader('Access-Control-Allow-Headers:x-requested-with,content-type');\nheader('content-type:application\/json'); echo json_encode($ret);\n?&gt;\n\/\/test.html \u5ba2\u6237\u7aef\u8bf7\u6c42\u9875\u9762\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"content-type\" content=\"text\/html;charset=utf-8\"&gt;\n&lt;script src=\"\/\/code.jquery.com\/jquery-1.11.0.min.js\"&gt;&lt;\/script&gt;\n&lt;title&gt; ajax \u8de8\u57df\u8bbf\u95eecookie\u4e22\u5931\u7684\u89e3\u51b3\u65b9\u6cd5 &lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;script type=\"text\/javascript\"&gt;\n$(function(){\n$.ajax({\nurl: 'http:\/\/b.fdipzone.com\/server.php', \/\/ \u8de8\u57df\ndataType: 'json',\ntype: 'post',\ndata: {'name':'fdipzone'}, success:function(ret){ if(ret&#91;'success']==true){ alert('cookie:' + ret&#91;'cookie']);\n}\n}\n});\n})\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"has-black-color has-text-color\">\u9996\u5148\u5148\u6267\u884chttp:\/\/b.fdipzone.com\/setcookie.php, \u521b\u5efa\u670d\u52a1\u7aefcookie\u3002\u7136\u540e\u6267\u884chttp:\/\/a.fdipzone.com\/test.html\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"success\":true,\"name\":\"fdipzone\",\"cookie\":\"\"}<\/code><\/pre>\n\n\n\n<p>\u83b7\u53d6cookie\u5931\u8d25\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u89e3\u51b3\u529e\u6cd5<\/h3>\n\n\n\n<p>\u8bf7\u6c42\u65f6\u5c06withCredentials\u5c5e\u6027\u8bbe\u7f6e\u4e3atrue\uff0c\u4f7f\u53ef\u4ee5\u6307\u5b9a\u67d0\u4e2a\u8bf7\u6c42\u5e94\u8be5\u53d1\u9001\u51ed\u636e\u3002\u82e5\u670d\u52a1\u5668\u63a5\u6536\u5e26\u51ed\u636e\u7684\u8bf7\u6c42\uff0c\u4f1a\u7528\u5982\u4e0bHTTP\u5934\u90e8\u6765\u54cd\u5e94\u3002<\/p>\n\n\n\n<p>\u670d\u52a1\u7aef-\u8bbe\u7f6eheader\uff1a\uff08\u5141\u8bb8\u8bf7\u6c42\u5e26\u6709\u9a8c\u8bc1\u4fe1\u606f\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header(\"Access-Control-Allow-Credentials:true\");<\/code><\/pre>\n\n\n\n<p>test.html \u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"content-type\" content=\"text\/html;charset=utf-8\"&gt;\n&lt;script src=\"\/\/code.jquery.com\/jquery-1.11.0.min.js\"&gt;&lt;\/script&gt;\n&lt;title&gt; ajax \u8de8\u57df\u8bbf\u95eecookie\u4e22\u5931\u7684\u89e3\u51b3\u65b9\u6cd5 &lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;script type=\"text\/javascript\"&gt;\n$(function(){\n$.ajax({\nurl: 'http:\/\/b.fdipzone.com\/server.php', \/\/ \u8de8\u57dfxhrFields:{withCredentials: true}, \/\/ \u53d1\u9001\u51ed\u636edataType: 'json',\ntype: 'post',\ndata: {'name':'fdipzone'}, success:function(ret){ if(ret&#91;'success']==true){ alert('cookie:' + ret&#91;'cookie']);\n}\n}\n});\n \n})\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>server.php\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$name = isset($_POST&#91;'name'])? $_POST&#91;'name'] : '';\n$ret = array( 'success' =&gt; true, 'name' =&gt; $name,\n'cookie' =&gt; isset($_COOKIE&#91;'data'])? $_COOKIE&#91;'data'] : ''\n);\n\/\/ \u6307\u5b9a\u5141\u8bb8\u5176\u4ed6\u57df\u540d\u8bbf\u95ee\nheader('Access-Control-Allow-Origin:http:\/\/a.fdipzone.com');\n\/\/ \u54cd\u5e94\u7c7b\u578b\nheader('Access-Control-Allow-Methods:POST');\n\/\/ \u54cd\u5e94\u5934\u8bbe\u7f6e\nheader('Access-Control-Allow-Headers:x-requested-with,content-type');\n\/\/ \u662f\u5426\u5141\u8bb8\u8bf7\u6c42\u5e26\u6709\u9a8c\u8bc1\u4fe1\u606f\nheader('Access-Control-Allow-Credentials:true');\nheader('content-type:application\/json'); echo json_encode($ret);\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>\u6309\u4e4b\u524d\u6b65\u9aa4\u6267\u884c\uff0c\u8bf7\u6c42\u8fd4\u56de\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"success\":true,\"name\":\"fdipzone\",\"cookie\":\"1484558863\"}<\/code><\/pre>\n\n\n\n<p>\u83b7\u53d6cookie\u6210\u529f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>  (1.\u5982\u679c\u5ba2\u6237\u7aef\u8bbe\u7f6e\u4e86withCredentials\u5c5e\u6027\u8bbe\u7f6e\u4e3atrue\uff0c\u800c\u670d\u52a1\u7aef\u6ca1\u6709\u8bbe\u7f6eAccess-Control-Allow-Credentials:true\uff0c\u8bf7\u6c42\u65f6\u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/strong><\/h4>\n\n\n\n<p>Reference:XMLHttpRequest cannot load <span style=\"text-decoration: underline;\">http:\/\/b.fdipzone.com\/server.php<\/span>. Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http:\/\/a.fdipzone.com' is therefore not allowed access.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>  (2.\u670d\u52a1\u7aefheader\u8bbe\u7f6eAccess-Control-Allow-Credentials:true\u540e\uff0cAccess-Control-Allow-Origin\u4e0d\u53ef\u4ee5\u8bbe\u4e3a*\uff0c\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u57df\u540d\uff0c\u5426\u5219\u56de\u8fd4\u56de\u9519\u8bef\u3002<\/strong><\/h4>\n\n\n\n<p>Reference:XMLHttpRequest cannot load <a href=\"http:\/\/b.fdipzone.com\/server.php\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/b.fdipzone.com\/server.php. <\/a>A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' heade<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.\u6269\u5c55\uff1a<strong>Ajax<\/strong>\u8de8\u57df\u8bf7\u6c42<strong>COOKIE<\/strong>\u65e0\u6cd5\u5e26\u4e0a\u7684\u89e3\u51b3\u529e\u6cd5<\/h3>\n\n\n\n<p>\u539f\u751f<strong>ajax<\/strong>\u8bf7\u6c42\u65b9\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var xhr = new XMLHttpRequest();\nxhr.open(\"POST\", \"http:\/\/xxxx.com\/demo\/b\/index.php\", true); xhr.withCredentials = true; \/\/ \u652f \u6301 \u8de8 \u57df \u53d1 \u9001 cookies xhr.send();\n<\/code><\/pre>\n\n\n\n<p><strong>jquery<\/strong>\u7684<strong>ajax<\/strong>\u7684<strong>post<\/strong>\u65b9\u6cd5\u8bf7\u6c42\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.ajax({\ntype: \"POST\",\nurl: \"http:\/\/xxx.com\/api\/test\", dataType:  'jsonp', xhrFields:  { withCredentials: true\n},\ncrossDomain: true, success:function(){\n},\nerror:function(){\n}\n})\n<\/code><\/pre>\n\n\n\n<p>\u670d\u52a1\u5668\u7aef\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<p>header(\"Access-Control-Allow-Credentials: true\"); <\/p>\n\n\n\n<p>header(\"Access-Control-Allow-Origin: <a href=\"http:\/\/www.xxx.com\/\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/www.xxx.com<\/a>\");<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.ajax\u8de8\u57df\u8bbf\u95ee,cookie\u4e22\u5931 \u521b\u5efa\u4e24\u4e2a\u6d4b\u8bd5\u57df\u540d\uff1aa.fdipzone.com \u4f5c\u4e3a\u5ba2\u6237\u7aef\u57df\u540d\uff0cb.fdipzone.co &#8230;<\/p>\n","protected":false},"author":1,"featured_media":24,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[8],"class_list":["post-14","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-frontend","tag-ajax"],"_links":{"self":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":3,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":25,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions\/25"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media\/24"}],"wp:attachment":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}