{"id":28400,"date":"2024-11-24T17:30:58","date_gmt":"2024-11-24T09:30:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28400\/"},"modified":"2024-11-24T17:30:58","modified_gmt":"2024-11-24T09:30:58","slug":"%e8%a6%81%e9%81%bf%e5%85%8d%e7%9a%84-fetch-%e5%92%8c-xmlhttp-%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28400\/","title":{"rendered":"\u8981\u907f\u514d\u7684 fetch() \u548c XMLHttp \u9519\u8bef"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173163420414631.jpg\" class=\"aligncenter\" title=\"\u8981\u907f\u514d\u7684 fetch() \u548c XMLHttp \u9519\u8bef\u63d2\u56fe\" alt=\"\u8981\u907f\u514d\u7684 fetch() \u548c XMLHttp \u9519\u8bef\u63d2\u56fe\" \/><\/p>\n<p>\u53ef\u60b2\u7684\u662f\uff0c\u6211\u6ca1\u6709\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\u3002\u6211\u5e0c\u671b\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u5176\u4ed6\u4eba\u5728\u5c1d\u8bd5<strong>\u66f4\u65b0\u7f51\u9875\u800c\u4e0d\u5b8c\u5168\u4e0b\u8f7d\u65b0\u7248\u672c<\/strong>\u65f6\u907f\u514d\u5b83\u4eec\u3002\u6211\u6700\u7ec8\u5f97\u5230\u7684\u4ee3\u7801\u4f3c\u4e4e\u6709\u6548\uff1a<\/p>\n<pre>async function fetchdbsingle(url, str) {\n  const datatosend = str;\n  console.log('fetchdbsingle: ' + str);\n\n  try {\n    const response = await fetch(url, {\n      method: 'post',\n      headers: { 'content-type': 'application\/x-www-form-urlencoded' },\n      body: datatosend\n    });\n\n    if (!response.ok) {\n      throw new error('network response was not ok');\n    }\n\n    const data = await response.json();\n    return data;\n  } catch (error) {\n    console.error('error fetching data:', error);\n    throw error; \/\/ re-throw the error to be handled by the caller\n  }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><em>awt<\/em> \u5728\u5f02\u6b65\u51fd\u6570\u5185\u5de5\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u5c1d\u8bd5\u8bbf\u95ee\u4e4b\u524d\u5df2\u5230\u8fbe\u3002\u5982\u679c\u60a8\u9700\u8981\u4ece\u666e\u901a\u4ee3\u7801\u8c03\u7528\u5f02\u6b65\u51fd\u6570\uff0c\u8bed\u6cd5\u4e3a <em>.then<\/em>:<\/p>\n<pre>fetchDbSingle(url, str).then(data =&gt; {\n  console.log(\"Received data:\", data);\n  \/\/ Use the data here\n}).catch(error =&gt; {\n  console.error(\"Error fetching data:\", error);\n}); \n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u60a8\u5c1d\u8bd5\u5728\u4e0d\u4f7f\u7528\u6b64\u7279\u6b8a\u8bed\u6cd5\u7684\u60c5\u51b5\u4e0b\u8bbf\u95ee\u6570\u636e\uff0c\u5219\u6570\u636e\u5c06\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u56e0\u4e3a\u60a8\u662f\u5728\u6570\u636e\u5230\u8fbe\u4e4b\u524d\u8bbf\u95ee\u5b83\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u6807\u8bb0\u4f4d\u7f6e\u4e4b\u5916\u7684\u6570\u636e\uff0c\u5b83\u5c06\u662f\u672a\u5b9a\u4e49\u7684\u3002<\/p>\n<p>\u5728\u6211\u7684\u7a0b\u5e8f\u4e2d\uff0cfetch() \u6b63\u5728\u8c03\u7528\u8bfb\u53d6\u6570\u636e\u5e93\u7684 php \u811a\u672c\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e9b\u8b66\u544a\uff0c\u5bf9\u4e8e\u6709\u7ecf\u9a8c\u7684\u4eba\u6765\u8bf4\u53ef\u80fd\u6beb\u65e0\u610f\u4e49\uff0c\u4f46\u6211\u5e0c\u671b\u6211\u65e9\u70b9\u77e5\u9053\uff1a<\/p>\n<ul>\n<li>\u8bf7\u6ce8\u610f\uff0cphp \u5c06\u901a\u8fc7 echo \u53d1\u9001\u6570\u636e\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cecho \u4e0d\u4f1a\u51fa\u73b0\u5728\u5c4f\u5e55\u4e0a\u3002<\/li>\n<li>\u786e\u4fdd\u60a8\u7684 php \u6587\u4ef6\u4ec5\u5305\u542b php \u4ee3\u7801\uff1b\u6ca1\u6709 html\u3002\u5982\u679c\u5b83\u5305\u542b html\uff0c\u5219\u8fd4\u56de\u7684\u6570\u636e\u5c06\u5305\u542b\u6240\u6709 html\uff0c\u8fd9\u5c06\u975e\u5e38\u6df7\u4e71\u3002 <\/li>\n<li>\u786e\u4fdd php \u6587\u4ef6\uff08\u53ca\u5176\u5305\u542b\u7684\u4efb\u4f55\u6587\u4ef6\uff09\u53ea\u6709\u4e00\u4e2a echo \u8bed\u53e5\u3002 \uff08\u54e6\uff0c\u5e76\u68c0\u67e5\u4efb\u4f55\u5305\u542b\u7684\u6587\u4ef6\u4e2d\u7684 html \u6216 echo\uff09<\/li>\n<li>json_encode \u5c06\u901a\u8fc7 echo \u53d1\u9001\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u80fd\u60f3\u8981 javascript json \u89e3\u6790\u5b83\u4ee5\u4f7f\u5176\u6210\u4e3a javascript \u6570\u7ec4\uff0c\u4f46\u8fd9\u4e0d\u662f\u5fc5\u9700\u7684\u3002<\/li>\n<\/ul>\n<p><em>\u5982\u679c\u6709\u4eba\u6709\u5174\u8da3\u77e5\u9053\u6211\u63d0\u5230\u4e0a\u8ff0\u8b66\u544a\uff0c\u6211\u53ef\u4ee5\u5199\u4e00\u7bc7\u6587\u7ae0\uff0c\u4ecb\u7ecd\u6211\u6240\u72af\u7684\u9519\u8bef\u4ee5\u53ca\u6211\u5982\u4f55\u82b1\u4e86\u4e00\u5468\u7684\u65f6\u95f4\u6765\u7ea0\u6b63\u8fd9\u4e9b\u9519\u8bef\uff0c\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u54af\u54af\u7b11\u5e76\u611f\u5230\u4f18\u8d8a\u3002<\/em><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8981\u907f\u514d\u7684 fetch() \u548c XMLHttp \u9519\u8bef\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u60b2\u7684\u662f\uff0c\u6211\u6ca1\u6709\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\u3002\u6211\u5e0c\u671b\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u5176\u4ed6\u4eba\u5728\u5c1d\u8bd5\u66f4\u65b0\u7f51\u9875\u800c\u4e0d\u5b8c\u5168\u4e0b\u8f7d\u65b0\u7248\u672c\u65f6\u907f\u514d\u5b83\u4eec\u3002\u6211\u6700\u7ec8\u5f97\u5230\u7684\u4ee3\u7801\u4f3c\u4e4e\u6709\u6548\uff1a async function fetchdbsingle(url, str) { const datatosend = str; console.log(&#8216;fetchdbsingle: &#8216; + str); try { const response = await fetch(url, { method: &#8216;post&#8217;, headers: { &#8216;content-type&#8217;: &#8216;application\/x-www-form-urlencoded&#8217; }, body: datatosend }); if (!response.ok) { throw new error(&#8216;network response was not ok&#8217;); } const data = await response.json(); return data; } catch (error) { console.error(&#8216;error [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-28400","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28400"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28400\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}