{"id":190949,"date":"2023-08-26T00:15:29","date_gmt":"2023-08-25T16:15:29","guid":{"rendered":"https:\/\/www.idc.net\/help\/190949\/"},"modified":"2023-08-26T00:15:29","modified_gmt":"2023-08-25T16:15:29","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91react%e6%95%99%e7%a8%8b%ef%bc%9a%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190949\/","title":{"rendered":"\u540e\u6d6a\u4e91React\u6559\u7a0b\uff1a\u7f51\u7edc"},"content":{"rendered":"<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">React Native \u7684\u4e00\u4e2a\u76ee\u6807\u662f\u6210\u4e3a\u4e00\u4e2a\u6e38\u4e50\u573a\u6240\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\u75af\u72c2\u7684\u60f3\u6cd5\u3002\u81ea\u4ece\u6d4f\u89c8\u5668\u4f7f\u7528\u8d77\u6765\u4e0d\u591f\u7075\u6d3b\uff0c\u6211\u4eec\u522b\u65e0\u9009\u62e9\uff0c\u53ea\u80fd\u53bb\u5b9e\u73b0\u6574\u4e2a\u5806\u6808\u3002\u5728\u8fd9\u4e2a\u6211\u4eec\u5e76\u4e0d\u6253\u7b97\u6539\u53d8\u4ec0\u4e48\u7684\u5730\u65b9\uff0c\u6211\u4eec\u8bd5\u56fe\u5c3d\u53ef\u80fd\u5fe0\u5b9e\u4e8e\u6d4f\u89c8\u5668\u7684 APIS\u3002\u7f51\u7edc\u534f\u8bae\u6808\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002<\/p>\n<h2>XMLHttpRequest<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">XMLHttpRequest API \u662f\u5728<span>&nbsp;<\/span>iOS networking apis<span>&nbsp;<\/span>\u4e4b\u4e0a\u5b9e\u73b0\u7684\u3002\u4e0e web \u663e\u8457\u7684\u533a\u522b\u662f\u5176\u5b89\u5168\u6a21\u5f0f\uff1a\u7531\u4e8e\u6ca1\u6709<span>&nbsp;<\/span>CORS\u7684\u6982\u5ff5\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e92\u8054\u7f51\u4e0a\u7684\u4efb\u4e00\u7f51\u7ad9\u4e0a\u8fdb\u884c\u9605\u8bfb\u3002<\/p>\n<pre>var&nbsp;request&nbsp;=&nbsp;new&nbsp;XMLHttpRequest();\nrequest.onreadystatechange&nbsp;=&nbsp;(e)&nbsp;=&gt;&nbsp;{&nbsp;&nbsp;if&nbsp;(request.readyState&nbsp;!==&nbsp;4)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return;\n&nbsp;&nbsp;}&nbsp;&nbsp;if&nbsp;(request.status&nbsp;===&nbsp;200)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.log('success',&nbsp;request.responseText);\n&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.warn('error');\n&nbsp;&nbsp;}\n};\nrequest.open('GET',&nbsp;'https:\/\/mywebsite.com\/endpoint.php');\nrequest.send();<\/pre>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u8bf7\u6309\u7167&nbsp;MDN Documentation\uff0c\u4e00\u4e2a\u5bf9 API \u8fdb\u884c\u4e86\u5b8c\u6574\u63cf\u8ff0\u7684\u6587\u6863\u3002<\/p>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u53d1\u4eba\u5458\uff0c\u4f60\u53ef\u80fd\u4e0d\u4f1a\u76f4\u63a5\u5c06 XMLHttpRequest \u5bf9\u8c61\u4f5c\u4e3a\u4ed6\u7684 API\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7e41\u7410\u7684\u5de5\u4f5c\u3002\u4f46\u4e8b\u5b9e\u4e0a\uff0c\u4ed6\u7684\u5b9e\u73b0\u548c\u4e0e\u6d4f\u89c8\u5668 APIS \u7684\u517c\u5bb9\u80fd\u591f\u4f7f\u4f60\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982\uff0c\u76f4\u63a5\u6765\u81ea npm \u7684&nbsp;Parse&nbsp;\u548c&nbsp;super-agent\u3002<\/p>\n<h2>Fetch<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">Fetch&nbsp;\u662f\u4e00\u79cd\u66f4\u597d\u7684\u7f51\u7edc API\uff0c\u5b83\u7684\u5de5\u4f5c\u662f\u901a\u8fc7\u6807\u51c6\u59d4\u5458\u4f1a\u5b8c\u6210\uff0c\u5e76\u4e14\u5df2\u7ecf\u5728\u706b\u72d0\u6d4f\u89c8\u5668\u4e0a\u53ef\u4ee5\u4f7f\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5728 React Native \u4e0a\u4e5f\u662f\u53ef\u7528\u7684\u3002<\/p>\n<pre>fetch('https:\/\/mywebsite.com\/endpoint.php')\n&nbsp;&nbsp;.then((response)&nbsp;=&gt;&nbsp;response.text())\n&nbsp;&nbsp;.then((responseText)&nbsp;=&gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.log(responseText);\n&nbsp;&nbsp;})\n&nbsp;&nbsp;.catch((error)&nbsp;=&gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.warn(error);\n&nbsp;&nbsp;});<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React Native \u7684\u4e00\u4e2a\u76ee\u6807\u662f\u6210\u4e3a\u4e00\u4e2a\u6e38\u4e50\u573a\u6240\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\u75af\u72c2\u7684\u60f3\u6cd5\u3002\u81ea\u4ece\u6d4f\u89c8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-190949","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190949"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190949\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}