{"id":43378,"date":"2023-12-11T14:59:39","date_gmt":"2023-12-11T06:59:39","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=43378"},"modified":"2023-12-11T14:59:39","modified_gmt":"2023-12-11T06:59:39","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0ajax%e8%af%b7%e6%b1%82%ef%bc%8c%e5%81%87%e5%a6%82%e6%88%91%e6%9c%89%e5%a4%9a%e4%b8%aa%e8%af%b7%e6%b1%82%ef%bc%8c%e6%88%91%e9%9c%80%e8%a6%81%e8%ae%a9%e8%bf%99","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0ajax%e8%af%b7%e6%b1%82%ef%bc%8c%e5%81%87%e5%a6%82%e6%88%91%e6%9c%89%e5%a4%9a%e4%b8%aa%e8%af%b7%e6%b1%82%ef%bc%8c%e6%88%91%e9%9c%80%e8%a6%81%e8%ae%a9%e8%bf%99\/","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0ajax\u8bf7\u6c42\uff0c\u5047\u5982\u6211\u6709\u591a\u4e2a\u8bf7\u6c42\uff0c\u6211\u9700\u8981\u8ba9\u8fd9\u4e9bajax\u8bf7\u6c42\u6309\u7167\u67d0\u79cd\u987a\u5e8f\u4e00\u6b21\u6267\u884c\uff0c\u6709\u4ec0\u4e48\u529e\u6cd5\u5462\uff1f\u5982\u4f55\u5904\u7406ajax\u8de8\u57df"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u53c2\u8003\u56de\u7b54\uff1a<\/p>\n<p>\u901a\u8fc7\u5b9e\u4f8b\u5316\u4e00\u4e2aXMLHttpRequest\u5bf9\u8c61\u5f97\u5230\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8c03\u7528\u5b9e\u4f8b\u7684open\u65b9\u6cd5\u4e3a\u8fd9\u6b21ajax\u8bf7\u6c42\u8bbe\u5b9a\u76f8\u5e94\u7684http\u65b9\u6cd5\uff0c\u76f8\u5e94\u7684\u5730\u5740\u548c\u662f\u5426\u5f02\u6b65\uff0c\u4ee5\u5f02\u6b65\u4e3a\u4f8b\uff0c\u8c03\u7528send\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u5b9a\u9700\u8981\u53d1\u9001\u7684\u62a5\u6587\u4e3b\u4f53\uff0c\u7136\u540e\u901a\u8fc7\u76d1\u542creadystatechange\u4e8b\u4ef6\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u4f8b \u7684readyState\u5c5e\u6027\u6765\u5224\u65ad\u8fd9\u4e2aajax\u8bf7\u6c42\u72b6\u6001\uff0c\u5176\u4e2d\u5206\u4e3a0\uff0c1\uff0c2\uff0c3\uff0c4\u8fd9\u56db\u79cd\u72b6\u6001\uff080\u672a\u521d\u59cb\u5316\uff0c1\u8f7d\u5165\/\u6b63\u5728\u53d1\u9001\u8bf7\u6c422\u8f7d\u5165\u5b8c\u6210\/\u6570\u636e\u63a5\u6536\uff0c3\u4ea4\u4e92\/\u89e3\u6790\u6570\u636e\uff0c4\u63a5\u6536\u6570\u636e\u5b8c\u6210\uff09\uff0c\u5f53\u72b6\u6001\u4e3a4\u7684\u65f6\u5019\u4e5f\u5c31\u662f\u63a5\u53d7\u6570\u636e\u5b8c\u6210\u7684\u65f6\u5019\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u7684status\u5c5e\u6027\u5224\u65ad\u8fd9\u4e2a\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-javascript&quot;&quot; lang=&quot;&quot;javascript&quot;&quot;&gt;var xhr = new XMLHttpRequest();<br \/>\nxhr.open(&#039;get&#039;, &#039;aabb.php&#039;, true);<br \/>\nxhr.send(null);<br \/>\nxhr.onreadystatechange = function() {<br \/>\n    if(xhr.readyState==4) {<br \/>\n        if(xhr.status==200) {<br \/>\n            console.log(xhr.responseText);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u4f7fajax\u8bf7\u6c42\u6309\u7167\u961f\u5217\u987a\u5e8f\u6267\u884c\uff0c\u901a\u8fc7\u8c03\u7528\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-javascript&quot;&quot; lang=&quot;&quot;javascript&quot;&quot;&gt;\/\/\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2aajax\u547d\u4ee4\uff0c\u56e0\u4e3a\u6570\u91cf\u4e0d\u5b9a\uff0c\u6240\u4ee5\u91c7\u7528\u9012\u5f52<br \/>\nfunction send(action, arg2) {<br \/>\n\/\/\u5c06\u591a\u4e2a\u547d\u4ee4\u6309\u987a\u5e8f\u5c01\u88c5\u6210\u6570\u7ec4\u5bf9\u8c61\uff0c\u9012\u5f52\u6267\u884c<br \/>\n\/\/\u5229\u7528\u4e86deferred\u5bf9\u8c61\u63a7\u5236\u56de\u8c03\u51fd\u6570\u7684\u7279\u70b9<br \/>\n    <span class=\"katex math inline\">.when(send_action(action[0], arg2))<br \/>\n    .done(function () {<br \/>\n\/\/\u524d\u4e00\u4e2aajax\u56de\u8c03\u51fd\u6570\u5b8c\u6bd5\u4e4b\u540e\u5224\u65ad\u961f\u5217\u957f\u5ea6<br \/>\nif (action.length>1) {<br \/>\n\/\/\u961f\u5217\u957f\u5ea6\u5927\u4e8e1\uff0c\u5219\u5f39\u51fa\u7b2c\u4e00\u4e2a\uff0c\u7ee7\u7eed\u9012\u5f52\u6267\u884c\u8be5\u961f\u5217<\/p>\n<p>    action.shift();<br \/>\n    send(action, arg2);<br \/>\n    }<br \/>\n    }).fail(function (){<br \/>\n    \/\/\u961f\u5217\u4e2d\u5143\u7d20\u8bf7\u6c42\u5931\u8d25\u540e\u7684\u903b\u8f91<br \/>\n    \/\/\u91cd\u8bd5\u53d1\u9001<br \/>\n    \/\/send(action, arg2);<br \/>\n    \/\/\u5ffd\u7565\u9519\u8bef\u8fdb\u884c\u4e0b\u4e2a<br \/>\n    \/\/if (action.length>1) {<br \/>\n    \/\/\u961f\u5217\u957f\u5ea6\u5927\u4e8e1\uff0c\u5219\u5f39\u51fa\u7b2c\u4e00\u4e2a\uff0c\u7ee7\u7eed\u9012\u5f52\u6267\u884c\u8be5\u961f\u5217<br \/>\n        \/\/    action.shift();<br \/>\n        \/\/    send(action, arg2);<br \/>\n        \/\/}<br \/>\n        });<br \/>\n        }<br \/>\n    \/\/\u5904\u7406\u6bcf\u4e2a\u547d\u4ee4\u7684ajax\u8bf7\u6c42\u4ee5\u53ca\u56de\u8c03\u51fd\u6570<\/p>\n<p>function send_action(command, arg2) {<br \/>\nvar dtd =<\/span>.Deferred();\/\/\u5b9a\u4e49deferred\u5bf9\u8c61<\/p>\n<p>    <span class=\"katex math inline\">.post(<br \/>\n    &quot;&quot;url&quot;&quot;,<br \/>\n    {<br \/>\n    command: command,<br \/>\n    arg2: arg2<br \/>\n    }<br \/>\n    ).done(function (json) {<br \/>\n    json =<\/span>.parseJSON(json);<\/p>\n<p>\/\/\u6bcf\u6b21\u8bf7\u6c42\u56de\u8c03\u51fd\u6570\u7684\u5904\u7406\u903b\u8f91<\/p>\n<p>    \/\/<br \/>\n    \/\/<br \/>\n    \/\/<br \/>\n\/\/\u903b\u8f91\u7ed3\u675f<br \/>\ndtd.resolve();<br \/>\n}).fail(function (){<br \/>\n\/\/ajax\u8bf7\u6c42\u5931\u8d25\u7684\u903b\u8f91<br \/>\ndtd.reject();<br \/>\n});<br \/>\nreturn dtd.promise();\/\/\u8fd4\u56deDeferred\u5bf9\u8c61\u7684promise\uff0c\u9632\u6b62\u5728\u5916\u90e8\u4fee\u6539\u72b6\u6001<\/p>\n<p>}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u53c2\u8003\u56de\u7b54\uff1a \u901a\u8fc7\u5b9e\u4f8b\u5316\u4e00\u4e2aXMLHttpRequest\u5bf9\u8c61\u5f97\u5230\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8c03\u7528\u5b9e [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-43378","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=43378"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43378\/revisions"}],"predecessor-version":[{"id":43379,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43378\/revisions\/43379"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=43378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=43378"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=43378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}