{"id":5823,"date":"2023-01-06T15:08:44","date_gmt":"2023-01-06T07:08:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5823"},"modified":"2023-01-06T15:08:44","modified_gmt":"2023-01-06T07:08:44","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8http%e8%ab%8b%e6%b1%82%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8http%e8%ab%8b%e6%b1%82%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528HTTP\u8acb\u6c42\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, HTTP, \u8acb\u6c42\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 HTTP \u8acb\u6c42\uff1f<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u5728 React \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 HTTP \u8acb\u6c42\u4f86\u8207\u4f3a\u670d\u5668\u9032\u884c\u4ea4\u4e92\uff0c\u4ee5\u7372\u53d6\u6578\u64da\u6216\u66f4\u65b0\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 HTTP \u8acb\u6c42\u3002<\/p>\n<h2>\u4f7f\u7528 XMLHttpRequest<\/h2>\n<p>XMLHttpRequest \u662f\u4e00\u500b\u7528\u65bc\u767c\u9001 HTTP \u8acb\u6c42\u7684 JavaScript API\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u91cd\u65b0\u52a0\u8f09\u6574\u500b\u9801\u9762\u7684\u60c5\u6cc1\u4e0b\u8207\u4f3a\u670d\u5668\u9032\u884c\u4ea4\u4e92\u3002XMLHttpRequest \u53ef\u4ee5\u7528\u65bc\u767c\u9001 GET\u3001POST\u3001PUT \u548c DELETE \u8acb\u6c42\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u6536 JSON \u683c\u5f0f\u7684\u6578\u64da\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 XMLHttpRequest \u767c\u9001 GET \u8acb\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst xhr = new XMLHttpRequest();\nxhr.open('GET', 'http:\/\/example.com\/api\/v1\/users');\nxhr.onload = () => {\n  if (xhr.status === 200) {\n    const data = JSON.parse(xhr.responseText);\n    \/\/ do something with the data\n  }\n};\nxhr.send();\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b XMLHttpRequest \u5c0d\u8c61\uff0c\u7136\u5f8c\u4f7f\u7528 open() \u65b9\u6cd5\u767c\u9001 GET \u8acb\u6c42\uff0c\u4e26\u5728 onload \u4e8b\u4ef6\u8655\u7406\u7a0b\u5e8f\u4e2d\u6aa2\u67e5\u8acb\u6c42\u7684\u72c0\u614b\u78bc\uff0c\u5982\u679c\u72c0\u614b\u78bc\u70ba 200\uff0c\u5247\u8868\u793a\u8acb\u6c42\u6210\u529f\uff0c\u7136\u5f8c\u5c07\u54cd\u61c9\u6587\u672c\u89e3\u6790\u70ba JSON \u683c\u5f0f\u7684\u6578\u64da\uff0c\u6700\u5f8c\u4f7f\u7528 send() \u65b9\u6cd5\u767c\u9001\u8acb\u6c42\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4f7f\u7528 Fetch API<\/h2>\n<p>Fetch API \u662f\u4e00\u500b\u65b0\u7684 JavaScript API\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u767c\u9001 HTTP \u8acb\u6c42\u3002Fetch API \u652f\u6301 GET\u3001POST\u3001PUT \u548c DELETE \u8acb\u6c42\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u6536 JSON \u683c\u5f0f\u7684\u6578\u64da\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 Fetch API \u767c\u9001 GET \u8acb\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfetch('http:\/\/example.com\/api\/v1\/users')\n  .then(response => response.json())\n  .then(data => {\n    \/\/ do something with the data\n  });\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 fetch() \u65b9\u6cd5\u767c\u9001 GET \u8acb\u6c42\uff0c\u7136\u5f8c\u4f7f\u7528 then() \u65b9\u6cd5\u5c07\u54cd\u61c9\u6587\u672c\u89e3\u6790\u70ba JSON \u683c\u5f0f\u7684\u6578\u64da\uff0c\u6700\u5f8c\u5c0d\u6578\u64da\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2>\u4f7f\u7528 axios<\/h2>\n<p>Axios \u662f\u4e00\u500b\u57fa\u65bc Promise \u7684 HTTP \u5ba2\u6236\u7aef\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u767c\u9001 HTTP \u8acb\u6c42\u3002Axios \u652f\u6301 GET\u3001POST\u3001PUT \u548c DELETE \u8acb\u6c42\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u6536 JSON \u683c\u5f0f\u7684\u6578\u64da\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 axios \u767c\u9001 GET \u8acb\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\naxios.get('http:\/\/example.com\/api\/v1\/users')\n  .then(response => {\n    const data = response.data;\n    \/\/ do something with the data\n  });\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 axios.get() \u65b9\u6cd5\u767c\u9001 GET \u8acb\u6c42\uff0c\u7136\u5f8c\u4f7f\u7528 then() \u65b9\u6cd5\u5c07\u54cd\u61c9\u6587\u672c\u89e3\u6790\u70ba JSON \u683c\u5f0f\u7684\u6578\u64da\uff0c\u6700\u5f8c\u5c0d\u6578\u64da\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 React \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 XMLHttpRequest\u3001Fetch API \u548c axios \u4f86\u767c\u9001 HTTP \u8acb\u6c42\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u6536 JSON \u683c\u5f0f\u7684\u6578\u64da\u3002\u6bcf\u7a2e\u65b9\u6cd5\u90fd\u6709\u5176\u512a\u9ede\u548c\u7f3a\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u81ea\u5df1\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528HTTP\u8acb\u6c42\uff0c\u5305\u62ec\u4f7f\u7528fetchAPI\u3001axios\u7b49\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u8655\u7406\u8acb\u6c42\u7684\u97ff\u61c9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5823","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1vV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5823"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5823\/revisions"}],"predecessor-version":[{"id":5824,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5823\/revisions\/5824"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}