{"id":3339,"date":"2024-11-10T09:05:33","date_gmt":"2024-11-10T01:05:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3339\/"},"modified":"2024-11-10T09:05:33","modified_gmt":"2024-11-10T01:05:33","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87js%e8%8e%b7%e5%8f%96ip","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3339\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7js\u83b7\u53d6ip"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u83b7\u53d6 ip \u5730\u5740\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u83b7\u53d6\u516c\u5171 ip \u5730\u5740 (fetch(&#8216;https:\/\/api.ipify.org?format=json&#8217;)&#8230;) \u548c\u83b7\u53d6\u672c\u5730 ip \u5730\u5740 (function getlocalip() {&#8230;})\u3002\u83b7\u53d6\u516c\u5171 ip \u5730\u5740\u9700\u8981\u542f\u7528 cors\uff0c\u800c\u83b7\u53d6\u672c\u5730 ip \u5730\u5740\u53ef\u80fd\u4f1a\u56e0\u5ba2\u6237\u7aef\u7981\u7528\u6216\u6d4f\u89c8\u5668\u51fa\u4e8e\u9690\u79c1\u8003\u8651\u963b\u6b62\u800c\u5931\u8d25\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110810031751002.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u901a\u8fc7js\u83b7\u53d6ip\u63d2\u56fe\" alt=\"\u5982\u4f55\u901a\u8fc7js\u83b7\u53d6ip\u63d2\u56fe\" \/><\/p>\n<p><strong>\u83b7\u53d6 IP \u5730\u5740\u7684 JavaScript \u65b9\u6cd5<\/strong><\/p>\n<p>\u83b7\u53d6\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u5bf9\u4e8e\u7f51\u7edc\u5e94\u7528\u548c\u5206\u6790\u81f3\u5173\u91cd\u8981\u3002\u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u76ee\u7684\u3002<\/p>\n<p><strong>\u83b7\u53d6\u516c\u5171 IP \u5730\u5740<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u516c\u5171 IP \u5730\u5740\uff08\u5373\u4ece\u4e92\u8054\u7f51\u4e0a\u53ef\u89c1\u7684 IP \u5730\u5740\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>fetch('https:\/\/api.ipify.org?format=json')\n  .then((res) =&gt; res.json())\n  .then((data) =&gt; console.log(data.ip))\n  .catch((error) =&gt; console.error(error));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u672c\u5730 IP \u5730\u5740<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u672c\u5730 IP \u5730\u5740\uff08\u5373\u5728\u672c\u5730\u7f51\u7edc\u4e0a\u53ef\u89c1\u7684 IP \u5730\u5740\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>function getLocalIP() {\n  const interfaces = require('os').networkInterfaces();\n  for (const name in interfaces) {\n    for (const address of interfaces[name]) {\n      if (\n        address.family === 'IPv4' &amp;&amp;\n        !address.internal &amp;&amp;\n        address.address !== '127.0.0.1'\n      ) {\n        return address.address;\n      }\n    }\n  }\n  return undefined;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 fetch() API \u83b7\u53d6\u516c\u5171 IP \u5730\u5740\u9700\u8981\u542f\u7528 CORS\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u53ef\u80fd\u7981\u7528\u5bf9\u672c\u5730 IP \u5730\u5740\u7684\u8bbf\u95ee\u3002<\/li>\n<li>\u51fa\u4e8e\u9690\u79c1\u8003\u8651\uff0c\u67d0\u4e9b\u6d4f\u89c8\u5668\u53ef\u80fd\u4f1a\u963b\u6b62\u83b7\u53d6\u672c\u5730 IP \u5730\u5740\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u901a\u8fc7js\u83b7\u53d6ip\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>javascript \u4e2d\u83b7\u53d6 ip \u5730\u5740\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u83b7\u53d6\u516c\u5171 ip \u5730\u5740 (fetch(&#8216;https:\/\/api.ipify.org?format=json&#8217;)&#8230;) \u548c\u83b7\u53d6\u672c\u5730 ip \u5730\u5740 (function getlocalip() {&#8230;})\u3002\u83b7\u53d6\u516c\u5171 ip \u5730\u5740\u9700\u8981\u542f\u7528 cors\uff0c\u800c\u83b7\u53d6\u672c\u5730 ip \u5730\u5740\u53ef\u80fd\u4f1a\u56e0\u5ba2\u6237\u7aef\u7981\u7528\u6216\u6d4f\u89c8\u5668\u51fa\u4e8e\u9690\u79c1\u8003\u8651\u963b\u6b62\u800c\u5931\u8d25\u3002 \u83b7\u53d6 IP \u5730\u5740\u7684 JavaScript \u65b9\u6cd5 \u83b7\u53d6\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u5bf9\u4e8e\u7f51\u7edc\u5e94\u7528\u548c\u5206\u6790\u81f3\u5173\u91cd\u8981\u3002\u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u76ee\u7684\u3002 \u83b7\u53d6\u516c\u5171 IP \u5730\u5740 \u8981\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u516c\u5171 IP \u5730\u5740\uff08\u5373\u4ece\u4e92\u8054\u7f51\u4e0a\u53ef\u89c1\u7684 IP \u5730\u5740\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a fetch(&#8216;https:\/\/api.ipify.org?format=json&#8217;) .then((res) =&gt; res.json()) .then((data) =&gt; console.log(data.ip)) .catch((error) =&gt; console.error(error)); \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6\u672c\u5730 IP \u5730\u5740 \u8981\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u672c\u5730 IP \u5730\u5740\uff08\u5373\u5728\u672c\u5730\u7f51\u7edc\u4e0a\u53ef\u89c1\u7684 IP \u5730\u5740\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a [&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-3339","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3339","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=3339"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3339\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}