{"id":167986,"date":"2024-05-08T16:14:16","date_gmt":"2024-05-08T08:14:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167986.html"},"modified":"2024-05-08T16:14:23","modified_gmt":"2024-05-08T08:14:23","slug":"javascript-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e6%b6%88-get-%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167986.html","title":{"rendered":"JavaScript \u4e2d\u5982\u4f55\u53d6\u6d88 get \u8bf7\u6c42"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034117\/3fa1c2d4-168e-4851-8fb0-acfe846863b7.webp\" alt=\"JavaScript \u4e2d\u5982\u4f55\u53d6\u6d88 get \u8bf7\u6c42\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u53d6\u6d88GET\u8bf7\u6c42\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<strong>\u4f7f\u7528AbortController \u548c Fetch API<\/strong>\u3001<strong>\u4f7f\u7528XMLHttpRequest\u5bf9\u8c61\u548conreadystatechange\u4e8b\u4ef6<\/strong>\u3002AbortController \u7ed3\u5408Fetch API\u662f\u66f4\u73b0\u4ee3\u7684\u65b9\u6848\uff0c\u4e3a\u4e86\u53d6\u6d88\u8bf7\u6c42\uff0c\u60a8\u9996\u5148\u521b\u5efa\u4e00\u4e2aAbortController\u5b9e\u4f8b\uff0c\u4e4b\u540e\u5728\u5176signal\u5c5e\u6027\u4f20\u7ed9fetch\u8c03\u7528\u3002\u5f53\u4f60\u8981\u53d6\u6d88\u8bf7\u6c42\u65f6\uff0c\u8c03\u7528AbortController\u5b9e\u4f8b\u7684abort\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528AbortController \u548c Fetch API<\/strong><\/p>\n<p>AbortController \u662f\u4e00\u79cd\u8f83\u65b0\u7684Web API\uff0c\u7528\u4e8e\u53d6\u6d88\u5c1a\u672a\u5b8c\u6210\u7684Web\u8bf7\u6c42\u3002\u4e0eFetch API\u642d\u914d\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u6709\u6548\u53d6\u6d88GET\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528AbortController \u53d6\u6d88Fetch\u8bf7\u6c42\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>\u521d\u59cb\u5316AbortController<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2aAbortController\u5b9e\u4f8b\uff0c\u8fd9\u5c06\u63d0\u4f9b\u4e00\u4e2asignal\u5bf9\u8c61\uff0c\u7528\u4e8e\u540e\u7eed\u4e0eFetch\u8bf7\u6c42\u7684\u5173\u8054\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const controller = new AbortController();<\/p>\n<p>const { signal } = controller;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7ed1\u5b9aSignal\u5230Fetch\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5728Fetch\u8bf7\u6c42\u4e2d\u6307\u5b9asignal\u5c5e\u6027\uff0c\u5c06AbortController\u4e0e\u8bf7\u6c42\u7ed1\u5b9a\u3002\u82e5\u8bf7\u6c42\u9047\u5230abort\u4e8b\u4ef6\uff0c\u5219\u4f1a\u88ab\u53d6\u6d88\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">fetch(url, { signal })<\/p>\n<p>  .then(response =&gt; response.json())<\/p>\n<p>  .then(data =&gt; console.log(data))<\/p>\n<p>  .catch(error =&gt; {<\/p>\n<p>    if (error.name === &#039;AbortError&#039;) {<\/p>\n<p>      \/\/ \u5904\u7406\u8bf7\u6c42\u88ab\u53d6\u6d88\u7684\u60c5\u51b5<\/p>\n<p>    } else {<\/p>\n<p>      \/\/ \u5904\u7406\u5176\u4ed6\u9519\u8bef<\/p>\n<p>    }<\/p>\n<p>  });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53d6\u6d88\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u53d6\u6d88\u8bf7\u6c42\u65f6\uff0c\u8c03\u7528AbortController\u5b9e\u4f8b\u7684abort\u65b9\u6cd5\u3002\u4e4b\u540eFetch\u8bf7\u6c42\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u5176name\u5c5e\u6027\u4e3a&#039;AbortError&#039;\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">controller.abort();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528XMLHttpRequest\u5bf9\u8c61\u548conreadystatechange\u4e8b\u4ef6<\/strong><\/p>\n<p>\u5728XMLHttpRequest (XHR)\u7684\u65e9\u671f\u7248\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539readyState\u6216\u8c03\u7528abort\u65b9\u6cd5\u6765\u53d6\u6d88GET\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528XHR\u53d6\u6d88GET\u8bf7\u6c42\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>\u521d\u59cb\u5316XMLHttpRequest\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aXMLHttpRequest\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const xhr = new XMLHttpRequest();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u76d1\u542c\u8bf7\u6c42\u72b6\u6001\u53d8\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u76d1\u542conreadystatechange\u4e8b\u4ef6\uff0c\u4ee5\u4fbf\u5728\u8bf7\u6c42\u4e0d\u518d\u9700\u8981\u65f6\u53d6\u6d88\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">xhr.onreadystatechange = function () {<\/p>\n<p>  if (this.readyState === 4 &amp;&amp; this.status === 200) {<\/p>\n<p>      \/\/ \u8bf7\u6c42\u6210\u529f<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528open\u548csend\u65b9\u6cd5\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">xhr.open(&#039;GET&#039;, url, true);<\/p>\n<p>xhr.send();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53d6\u6d88\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u53d6\u6d88\u8bf7\u6c42\uff0c\u8c03\u7528abort\u65b9\u6cd5\u5373\u53ef\u7acb\u5373\u7ec8\u6b62\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">xhr.abort();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u80fd\u6709\u6548\u53d6\u6d88\u53d1\u9001\u4e2d\u7684GET\u8bf7\u6c42\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5229\u7528AbortController\u53d6\u6d88Fetch\u8bf7\u6c42\u662f\u66f4\u73b0\u4ee3\u548c\u63a8\u8350\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u4e14\u662f\u6807\u51c6\u5316\u7684\u65b9\u6cd5\u3002\u4f46\u5728\u4e00\u4e9b\u65e7\u7684\u4ee3\u7801\u5e93\u6216\u8005\u9700\u8981\u652f\u6301\u4e0d\u517c\u5bb9Fetch API\u7684\u65e7\u6d4f\u89c8\u5668\u7684\u60c5\u51b5\u4e0b\uff0cXMLHttpRequest\u4f9d\u65e7\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f8b\u5982\u5728\u4e00\u4e2a\u590d\u6742\u7684\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u4e2d\uff0c\u53d6\u6d88\u8bf7\u6c42\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u9700\u6c42\u3002\u6bd4\u5982\uff0c\u5728\u7528\u6237\u5feb\u901f\u5207\u6362\u9875\u9762\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u53d6\u6d88\u4e0a\u4e00\u4e2a\u9875\u9762\u4e0a\u672a\u5b8c\u6210\u7684\u7f51\u7edc\u8bf7\u6c42\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u8282\u7ea6\u670d\u52a1\u5668\u8d44\u6e90\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u7f51\u7edc\u6d41\u91cf\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u4fdd\u8bc1\u7528\u6237\u4f53\u9a8c\u7684\u6d41\u7545\u6027\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a<\/strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u7ec8\u6b62\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong>\u8981\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 XMLHttpRequest \u5bf9\u8c61\u63d0\u4f9b\u7684 <code>abort()<\/code> \u65b9\u6cd5\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a XMLHttpRequest \u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u53d1\u8d77 GET \u8bf7\u6c42\u3002\u5f53\u4f60\u60f3\u8981\u53d6\u6d88\u8bf7\u6c42\u65f6\uff0c\u8c03\u7528 <code>abort()<\/code> \u65b9\u6cd5\u5373\u53ef\u3002\u8fd9\u5c06\u5bfc\u81f4\u8bf7\u6c42\u88ab\u7acb\u5373\u7ec8\u6b62\uff0c\u5e76\u89e6\u53d1 <code>readystatechange<\/code> \u4e8b\u4ef6\u7684 <code>abort<\/code> \u72b6\u6001\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a<\/strong>\u6211\u5728 JavaScript \u4e2d\u60f3\u8981\u7ec8\u6b62\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\uff0c\u5e94\u8be5\u6ce8\u610f\u4ec0\u4e48\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong>\u5f53\u4f60\u60f3\u8981\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\u65f6\uff0c\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5728\u8c03\u7528 <code>abort()<\/code> \u65b9\u6cd5\u4e4b\u524d\uff0c\u8981\u5148\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u5df2\u7ecf\u6210\u529f\u53d1\u51fa\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5 XMLHttpRequest \u5bf9\u8c61\u7684 <code>readyState<\/code> \u5c5e\u6027\u503c\u662f\u5426\u662f <code>1<\/code> (\u4ee3\u8868\u5df2\u7ecf open \u4f46\u5c1a\u672a\u53d1\u9001) \u6216 <code>2<\/code> (\u4ee3\u8868\u5df2\u7ecf\u53d1\u9001\u4f46\u5c1a\u672a\u63a5\u6536\u54cd\u5e94)\u3002\u5176\u6b21\uff0c\u5728\u8c03\u7528 <code>abort()<\/code> \u65b9\u6cd5\u4e4b\u540e\uff0c\u9700\u8981\u901a\u8fc7\u68c0\u67e5 XMLHttpRequest \u5bf9\u8c61\u7684 <code>readyState<\/code> \u5c5e\u6027\u662f\u5426\u4e3a <code>0<\/code> (\u4ee3\u8868\u8bf7\u6c42\u5df2\u7ecf\u7ec8\u6b62) \u6765\u786e\u8ba4\u8bf7\u6c42\u662f\u5426\u6210\u529f\u53d6\u6d88\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a<\/strong>\u9664\u4e86\u4f7f\u7528 XMLHttpRequest \u5bf9\u8c61\u7684 <code>abort()<\/code> \u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\u5417\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong>\u662f\u7684\uff0c\u9664\u4e86\u4f7f\u7528 XMLHttpRequest \u5bf9\u8c61\u7684 <code>abort()<\/code> \u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684 GET \u8bf7\u6c42\u3002\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5c06\u8bf7\u6c42\u5c01\u88c5\u5728\u4e00\u4e2a Promise \u4e2d\uff0c\u5e76\u4f7f\u7528 <code>fetch()<\/code> \u51fd\u6570\u6765\u5904\u7406\u8bf7\u6c42\u3002\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u5c1a\u672a\u89e3\u51b3\u7684 Promise \u5bf9\u8c61\uff0c\u5e76\u5728\u9700\u8981\u53d6\u6d88\u8bf7\u6c42\u65f6\uff0c\u4f7f\u7528 <code>reject()<\/code> \u65b9\u6cd5\u6765\u62d2\u7edd Promise\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u53d6\u6d88 GET \u8bf7\u6c42\u7684\u6548\u679c\u3002\u53e6\u5916\uff0c\u5982\u679c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216\u6846\u67b6\uff0c\u5982 Axios \u6216 jQuery\uff0c\u5b83\u4eec\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684\u8bf7\u6c42\u3002\u5982\u679c\u4f60\u5728\u9879\u76ee\u4e2d\u5df2\u7ecf\u4f7f\u7528\u4e86\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u67e5\u9605\u5b83\u4eec\u7684\u6587\u6863\u6765\u4e86\u89e3\u5982\u4f55\u53d6\u6d88 GET \u8bf7\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u53d6\u6d88GET\u8bf7\u6c42\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528AbortController \u548c Fetch [&hellip;]","protected":false},"author":3,"featured_media":167994,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167986"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=167986"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167986\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167994"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}