{"id":2022,"date":"2023-04-01T19:21:10","date_gmt":"2023-04-01T11:21:10","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2022"},"modified":"2023-04-07T10:35:56","modified_gmt":"2023-04-07T02:35:56","slug":"differences-in-content-type-parameter-settings-for-jquery-ajax-request-in-springmvc-jsp-between-application-json-and-application-x-www-form-urlencoded","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/differences-in-content-type-parameter-settings-for-jquery-ajax-request-in-springmvc-jsp-between-application-json-and-application-x-www-form-urlencoded\/","title":{"rendered":"SpringMVC+JSP\u4e2d\u5173\u4e8eJQuery ajax\u63d0\u4ea4\u7684Content-Type\u53c2\u6570\u8bbe\u7f6eapplication\/json\u548capplication\/x-www-form-urlencoded\u533a\u522b"},"content":{"rendered":"<p>\u9875\u9762\u662f\u7528\u7684JSP\uff0c\u540e\u53f0\u7528\u7684Spring MVC<\/p>\n<p>\u4f7f\u7528JQuery\u7684ajax\u9700\u8981\u8bbe\u7f6eContent-Type\uff0cContent-Type\u7684\u8bbe\u7f6e\u6709\u4ee5\u4e0b\u51e0\u79cd\u5e38\u7528\u7684<\/p>\n<p><!-- more --><\/p>\n<pre><code>&quot;Content-Type&quot;: &quot;application\/x-www-form-urlencoded&quot;    \/\/\u9002\u7528\u4e8e\u5927\u90e8\u5206\u60c5\u51b5\n&quot;Content-Type&quot;: &quot;application\/json&quot;    \/\/\u9002\u7528\u4e8e\u590d\u6742JSON\u6570\u636e\u7684\u63d0\u4ea4\n&quot;Content-Type&quot;: &quot;multipart\/form-data&quot;    \/\/\u9002\u7528\u4e8e\u6587\u4ef6\u4e0a\u4f20<\/code><\/pre>\n<h3>application\/x-www-form-urlencoded<\/h3>\n<p><code>application\/x-www-form-urlencoded<\/code>\u662fJQuery ajax\u9ed8\u8ba4\u7684\u63d0\u4ea4\u65b9\u5f0f\uff0c\u5f53\u4e0d\u5199contentType\u65f6\u5373\u662f\u6b64\u79cd\u65b9\u5f0f\uff0c\u4ee3\u8868\u4f7f\u7528\u8868\u5355\u5f62\u5f0f\u63d0\u4ea4\u3002 <\/p>\n<p>JSP\uff1a<\/p>\n<pre><code class=\"language-javascript\">$.ajax({\n  type: &quot;POST&quot;,\n  url: &quot;${basePath}\/comment&quot;,\n  contentType: &quot;application\/x-www-form-urlencoded&quot;, \n  dataType: &quot;json&quot;, \/\/\u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4e0d\u5fc5\u987b\n  data: {comment: &#039;appblog.cn&#039;},\n  success: function (result) {\n    alert(result);\n  }\n});<\/code><\/pre>\n<p>\u540e\u53f0\uff1a<\/p>\n<pre><code class=\"language-java\">@RequestMapping(value = &quot;\/order\/{orderId}&quot;, method = RequestMethod.POST)\n@ResponseBody\npublic Result reviewOrder(@PathVariable(&quot;orderId&quot;) Long orderId, HttpServletRequest request) {\n    String comment = request.getParameter(&quot;comment&quot;);\n\n}<\/code><\/pre>\n<h3>application\/json<\/h3>\n<p>\u4f7f\u7528<code>application\/x-www-form-urlencoded<\/code>\u53ea\u80fd\u63d0\u4ea4\u7b80\u5355\u7c7b\u578b\u7684json\u6570\u636e\uff0c\u5f53json\u6570\u636e\u5f88\u590d\u6742\u65f6\u5c31\u987b\u8981\u4f7f\u7528<code>application\/json<\/code><\/p>\n<p>JSP\uff1a<\/p>\n<pre><code class=\"language-javascript\">$.ajax({\n  type: &quot;POST&quot;,\n  url: &quot;${basePath}\/comment&quot;,\n  contentType: &quot;application\/json&quot;, \/\/\u5fc5\u987b\u6709\n  dataType: &quot;json&quot;, \/\/\u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4e0d\u5fc5\u987b\n  data: JSON.stringify({&#039;comment&#039;: &#039;appblog.cn&#039;}),\n  success: function (result) {\n    alert(result);\n  }\n});<\/code><\/pre>\n<p>\u540e\u53f0<\/p>\n<pre><code class=\"language-java\">@RequestMapping(value = &quot;\/order\/{orderId}&quot;, method = RequestMethod.POST)\n@ResponseBody\npublic Result reviewOrder(@PathVariable(&quot;orderId&quot;) Long orderId, @RequestBody Review review, HttpServletRequest request) {\n    String comment1 = request.getParameter(&quot;ids&quot;);    \/\/\u8f93\u51fanull\n    String comment2 = review.getComment();\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class Review {\n    String comment;\n\n    public String getComment() {\n        return comment;\n    }\n\n    public void setComment(String comment) {\n        this.comment = comment;\n    }\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528JSONObject<\/p>\n<pre><code class=\"language-java\">@RequestMapping(value = &quot;\/order\/{orderId}&quot;, method = RequestMethod.POST)\n@ResponseBody\npublic Result reviewOrder(@PathVariable(&quot;orderId&quot;) Long orderId, @RequestBody JSONObject object, HttpServletRequest request) {\n    String comment = object.getString(&quot;comment&quot;);\n}<\/code><\/pre>\n<p>\u603b\u7ed3\uff1a\u5f53\u4f7f\u7528<code>application\/x-www-form-urlencoded<\/code>\u65f6\u5176\u5b9eJSP\u4f1a\u9ed8\u8ba4\u628ajson\u6570\u636e\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4f7f\u7528<code>application\/json<\/code>\u65f6\u9700\u8981\u5411\u540e\u53f0\u4f20\u5165\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u8981\u7528JSON.stringify\u51fd\u6570\u628aJSON\u5bf9\u8c61\u8f6c\u6210json\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9875\u9762\u662f\u7528\u7684JSP\uff0c\u540e\u53f0\u7528\u7684Spring MVC \u4f7f\u7528JQuery\u7684ajax\u9700\u8981\u8bbe\u7f6eContent-Type\uff0cC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[83],"class_list":["post-2022","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-ajax"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2022"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2022\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}