{"id":1598,"date":"2023-03-25T20:05:06","date_gmt":"2023-03-25T12:05:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1598"},"modified":"2023-04-27T21:25:37","modified_gmt":"2023-04-27T13:25:37","slug":"explanation-of-vue-axios-post-sending-string-key-value-to-the-backend-requestparam-receiving-null-and-using-application-x-www-form-urlencoded","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/explanation-of-vue-axios-post-sending-string-key-value-to-the-backend-requestparam-receiving-null-and-using-application-x-www-form-urlencoded\/","title":{"rendered":"Vue axios POST\u4f20 \u5b57\u7b26\u4e32\/\u952e\u503c\u5bf9 \u540e\u53f0@RequestParam\u63a5\u6536\u4e3a\u7a7a \u53ca\u4f7f\u7528application\/x-www-form-urlencoded\u95ee\u9898\u8be6\u89e3"},"content":{"rendered":"<h2>\u539f\u56e0\u5206\u6790<\/h2>\n<p>axios post\u4f7f\u7528<code>application\/x-www-form-urlencoded<\/code>\u95ee\u9898\uff1a<\/p>\n<p>\u9ed8\u8ba4<code>application\/x-www-form-urlencoded<\/code>\u5bf9\u8868\u5355\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u6570\u636e\u4ee5\u952e\u503c\u5bf9\u901a\u8fc7<code>&amp;<\/code>\u7b26\u53f7\u62fc\u63a5\u5728http\u8bf7\u6c42\u4f53\u4e2d\u53d1\u9001\u7ed9\u670d\u52a1\u5668<\/p>\n<p><!-- more --><\/p>\n<p>\u5982\u679c\u5c5e\u6027\u4e3a<code>multipart\/form-data<\/code>\uff0c\u5219\u4ee5\u6d88\u606f\u7684\u5f62\u5f0f\u53d1\u9001\u7ed9\u670d\u52a1\u5668<\/p>\n<p>\u4f46\u662f\u6211\u4eec\u5728vue\u91cc\u9762\u76f4\u63a5\u63d0\u4ea4\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6839\u672c\u4e0d\u662f\u8fd9\u4e2a\u73a9\u610f\uff0c\u670d\u52a1\u7aef\u4e2d\u9700\u4ee5<code>@RequestBody<\/code>\u624d\u80fd\u8fdb\u884c\u63a5\u6536<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u8981\u501f\u52a9<code>node<\/code>\u91cc\u9762\u7684<code>qs<\/code>\u5e93<\/p>\n<p><code>qs<\/code>\u548c<code>JSON<\/code>\u5dee\u4e0d\u591a\uff08\u4f46\u662f<code>qs.stringify<\/code>\u548c<code>JSON.stringify<\/code>\u7ed3\u679c\u4e0d\u540c\uff0c\u53ef<code>console<\/code>\u6253\u5370\u67e5\u770b\uff09<\/p>\n<pre><code class=\"language-javascript\">var params = {}\nparams['subject'] = 'unoin-pay-subject'\nparams['body'] = 'unoin-pay-body'\nconsole.log(JSON.stringify(params))\nconsole.log(this.qs.stringify(params))<\/code><\/pre>\n<pre><code class=\"language-javascript\">{\"subject\":\"unoin-pay-subject\",\"body\":\"unoin-pay-body\"}\nsubject=unoin-pay-subject&body=unoin-pay-body<\/code><\/pre>\n<h2>\u6b63\u786e\u59ff\u52bf<\/h2>\n<h3>\u76f8\u5173\u4ee3\u7801<\/h3>\n<pre><code class=\"language-javascript\">let data = {\n  \"userId\": userId,\n};\n\nlet config = {\n  headers: {\n    \"Content-Type\": \"application\/x-www-form-urlencoded\",\n  },\n};\n\n_this.$axios.post(url, _this.qs.stringify(data), config).then(function (res) {...<\/code><\/pre>\n<h3>\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<p>1.\u6b63\u5e38\u5b9a\u4e49\u8bf7\u6c42\u4f53\u53c2\u6570\u5bf9\u8c61<\/p>\n<p>2.\u901a\u8fc7axios\u7684config\u914d\u7f6e`Content-Type`\u4e3a`application\/x-www-form-urlencoded`<\/p>\n<p>\u66f4\u591aconfig\u914d\u7f6e\u8bf4\u660e\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.axios-js.com\/docs\/\">http:\/\/www.axios-js.com\/docs\/<\/a><\/p>\n<p>3.qs.stringify\u5e8f\u5217\u5316\u6570\u636e<\/p>\n<h3>qs\u5f15\u5165\u6b65\u9aa4<\/h3>\n<p>1.\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-javascript\">npm i qs --save\n```\u00a0\n\n2.&amp;#x60;main.js&amp;#x60;\u5f15\u5165\uff1a\n\n```javascript\nimport qs from 'qs'<\/code><\/pre>\n<p>3.\u5168\u5c40\u58f0\u660e\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5168\u5c40\u5f15\u5165qs\u4f20\u8f93\u6570\u636e\u5e8f\u5217\u5316\nVue.prototype.qs=qs;<\/code><\/pre>\n<p>4.\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-javascript\">this.qs.stringify(data)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u56e0\u5206\u6790 axios post\u4f7f\u7528application\/x-www-form-urlencoded\u95ee\u9898\uff1a \u9ed8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[290],"class_list":["post-1598","post","type-post","status-publish","format-standard","hentry","category-vue","tag-axios"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1598","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=1598"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1598\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}