{"id":37094,"date":"2024-11-26T12:49:52","date_gmt":"2024-11-26T04:49:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37094\/"},"modified":"2024-11-26T12:49:52","modified_gmt":"2024-11-26T04:49:52","slug":"ajax%e6%80%8e%e4%b9%88%e4%bc%a0%e6%95%b0%e7%bb%84%e5%88%b0java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37094\/","title":{"rendered":"ajax\u600e\u4e48\u4f20\u6570\u7ec4\u5230java"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 java\uff1a\u5c06 javascript \u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u3002\u901a\u8fc7 ajax \u53d1\u9001 json \u5b57\u7b26\u4e32\u5230 java \u670d\u52a1\u5668\u3002\u5728 java \u7aef\u4f7f\u7528 json \u5e93\u89e3\u6790 json \u5b57\u7b26\u4e32\u5e76\u8f6c\u6362\u4e3a java \u6570\u7ec4\u3002\u5c06 java \u6570\u7ec4\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u4f5c\u4e3a json \u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111301002481004.jpg\" class=\"aligncenter\" title=\"ajax\u600e\u4e48\u4f20\u6570\u7ec4\u5230java\u63d2\u56fe\" alt=\"ajax\u600e\u4e48\u4f20\u6570\u7ec4\u5230java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7 Ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 Java<\/strong><\/p>\n<p><strong>\u4f7f\u7528 JSON \u8868\u793a\u6570\u7ec4<\/strong><\/p>\n<p>\u4e3a\u4e86\u901a\u8fc7 Ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 Java\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5 (JSON) \u5b57\u7b26\u4e32\u3002JSON \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6570\u636e\u683c\u5f0f\uff0c\u5141\u8bb8\u5728 JavaScript \u548c Java \u4e4b\u95f4\u9ad8\u6548\u4f20\u8f93\u6570\u636e\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a \"numbers\" \u7684 JavaScript \u6570\u7ec4\nconst numbers = [1, 2, 3, 4, 5];\n\n\/\/ \u5c06 \"numbers\" \u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\nconst jsonNumbers = JSON.stringify(numbers);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u901a\u8fc7 Ajax \u53d1\u9001 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528 Ajax (\u5982\u4f7f\u7528 XMLHttpRequest) \u5c06 JSON \u5b57\u7b26\u4e32\u53d1\u9001\u5230 Java \u670d\u52a1\u5668\u3002\u53d1\u9001\u7684\u8bf7\u6c42\u53ef\u4ee5\u5305\u542b JSON \u5b57\u7b26\u4e32\u4f5c\u4e3a\u8bf7\u6c42\u4e3b\u4f53\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u4f7f\u7528 XMLHttpRequest \u53d1\u9001 JSON \u5b57\u7b26\u4e32\nconst xhr = new XMLHttpRequest();\nxhr.open(\"POST\", \"your_server_url\");\nxhr.setRequestHeader(\"Content-Type\", \"application\/json\");\nxhr.send(jsonNumbers);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5728 Java \u4e2d\u63a5\u6536 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u5728 Java \u670d\u52a1\u5668\u7aef\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Jackson \u8fd9\u6837\u7684 JSON \u5e93\u6765\u89e3\u6790\u4f20\u5165\u7684 JSON \u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a Java \u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\n\/\/ \u63a5\u6536 JSON \u5b57\u7b26\u4e32\u4f5c\u4e3a\u8bf7\u6c42\u4e3b\u4f53\nString jsonNumbers = request.getReader().readLine();\n\n\/\/ \u4f7f\u7528 Jackson \u89e3\u6790 JSON \u5b57\u7b26\u4e32\u5e76\u8f6c\u6362\u4e3a Java \u6570\u7ec4\nObjectMapper mapper = new ObjectMapper();\nint[] numbersArray = mapper.readValue(jsonNumbers, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fd4\u56de\u6570\u7ec4<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5c06 Java \u6570\u7ec4\u6dfb\u52a0\u5230\u54cd\u5e94\u5bf9\u8c61\u5e76\u5c06\u5176\u4f5c\u4e3a JSON \u5b57\u7b26\u4e32\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u6765\u5c06\u6570\u7ec4\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u5c06 Java \u6570\u7ec4\u6dfb\u52a0\u5230\u54cd\u5e94\u5bf9\u8c61\nresponse.getWriter().write(mapper.writeValueAsString(numbersArray));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fajax\u600e\u4e48\u4f20\u6570\u7ec4\u5230java\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>\u901a\u8fc7 ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 java\uff1a\u5c06 javascript \u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u3002\u901a\u8fc7 ajax \u53d1\u9001 json \u5b57\u7b26\u4e32\u5230 java \u670d\u52a1\u5668\u3002\u5728 java \u7aef\u4f7f\u7528 json \u5e93\u89e3\u6790 json \u5b57\u7b26\u4e32\u5e76\u8f6c\u6362\u4e3a java \u6570\u7ec4\u3002\u5c06 java \u6570\u7ec4\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u4f5c\u4e3a json \u5b57\u7b26\u4e32\u3002 \u5982\u4f55\u901a\u8fc7 Ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 Java \u4f7f\u7528 JSON \u8868\u793a\u6570\u7ec4 \u4e3a\u4e86\u901a\u8fc7 Ajax \u5c06\u6570\u7ec4\u4f20\u9012\u5230 Java\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5 (JSON) \u5b57\u7b26\u4e32\u3002JSON \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6570\u636e\u683c\u5f0f\uff0c\u5141\u8bb8\u5728 JavaScript \u548c Java \u4e4b\u95f4\u9ad8\u6548\u4f20\u8f93\u6570\u636e\u3002 \u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;numbers&#8221; \u7684 JavaScript \u6570\u7ec4 const [&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-37094","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37094","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=37094"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37094\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}