{"id":391,"date":"2019-02-14T12:13:00","date_gmt":"2019-02-14T04:13:00","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=391"},"modified":"2022-05-17T09:43:58","modified_gmt":"2022-05-17T01:43:58","slug":"vueflask","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=391","title":{"rendered":"VUE+FLASK\u524d\u540e\u7aef\u5206\u79bb(\u56de\u7b54\u9762\u8bd5\u9898\u4e4b\u201c\u4f60\u6765\u8bf4\u8bf4restful\u524d\u540e\u7aef\u4ee3\u7801\u600e\u4e48\u5199\u201d)"},"content":{"rendered":"<h1><strong>\u52a8\u673a<\/strong><\/h1>\n<p>\u524d\u6bb5\u65f6\u95f4\u51fa\u53bb\u9762\u8bd5\uff0c\u9047\u5230\u4e86\u597d\u51e0\u6b21\u5bf9\u65b9\u9762\u8bd5\u5b98\u95ee\u8fd9\u6837\u7684\u95ee\u9898\uff0c&#8221;<strong><span style=\"color: #ff0000;\">restful\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u524d\u540e\u7aef\u600e\u4e48\u5199<\/span><\/strong>\uff1f&#8221;\uff0c\u201c<strong><span style=\"color: #ff0000;\">\u4ece\u6d4f\u89c8\u5668\u524d\u7aef\u5230\u540e\u53f0\u7ecf\u8fc7\u4e86\u54ea\u4e9b?\u8bf4\u7684\u8d8a\u8be6\u7ec6\u8d8a\u597d<\/span><\/strong>\u3002\u201d\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u6211\u542c\u5230\u7684\u7b2c\u4e00\u65f6\u523b\u662f\u61f5\u903c\u7684\uff0c\u5565\u60c5\u51b5\uff1f\u6211\u8981\u4ecevue\u7684\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\u5f00\u59cb\u8bf4\u5417\uff1faxios\u7684\u7528\u6cd5\u8981\u8bf4\u5417\uff1ffalsk\u7684restful\u662f\u5982\u4f55\u7528\u2018\u2019\/api\/\u2018\u2019\u5bf9\u5e94\u524d\u53f0\u7684url\u7684\u5417\uff1f\u8fd8\u662f\u53bb\u8bf4spring\u6846\u67b6\u7684mvc? <span style=\"color: #ff0000;\"><strong>\u4ea7\u751f\u8fd9\u6837\u7684\u7591\u60d1\uff0c\u4e3b\u8981\u539f\u56e0\u662f\uff0c\u6211\u4e0d\u660e\u767d\u9762\u8bd5\u5b98\u4e3a\u4ec0\u4e48\u8981\u95ee\u8fd9\u6837\u7684\u95ee\u9898\uff1f\u5b9e\u73b0\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u8bf4\u8d77\u6765\u53c8\u592a\u5bbd\u6cdb\uff0c\u4e0d\u77e5\u9053\u8bf4\u7684\u662f\u4e0d\u662f\u9762\u8bd5\u5b98\u60f3\u8981\u7684\u7b54\u6848\uff0c\u5bb9\u6613\u504f\u9898\u3002<\/strong><span style=\"color: #000000;\"> \u5728\u6211\u56de\u5934\u4ed4\u7ec6\u60f3\u60f3\u4e86\u4e4b\u540e\uff0c\u51b3\u5b9a\u4ee5\u540e\u518d\u9047\u5230\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u5c31\u4f7f\u7528vue+falsk\u505a\u4f8b\u5b50\u6765\u8bb2\u89e3\u8fd9\u4e2a\u3002<\/span><\/span><\/p>\n<h1><strong>\u56de\u7b54\u7b56\u7565<\/strong><\/h1>\n<p>\u6309\u7167\u4e0b\u9762\u7684\u51e0\u6b65\uff0c\u987a\u5e8f\u56de\u7b54<\/p>\n<ol>\n<li>\u4ee5vue+flask \u524d\u540e\u7aef\u5206\u79bb\u4e3a\u57fa\u7840\uff0c\u4ee5\u7528\u6237\u767b\u5f55\uff0c\u8f93\u5165\u7528\u6237\u540d\u5bc6\u7801\u4e3a\u573a\u666f\u3002<\/li>\n<li>vue\u524d\u7aef\u6846\u67b6\u901a\u8fc7v-model\u83b7\u5f97\u8f93\u5165\u6846\u8f93\u5165\u7684\u7528\u6237\u540d\u4ee5\u53ca\u5bc6\u7801\u3002\u901a\u8fc7\u5f15\u5165axios\u5411\u540e\u53f0\u53d1\u8d77http\u8bf7\u6c42\uff0caxios\u662f\u4e00\u4e2ahttp\u5e93\uff0c\u53ef\u4ee5\u5728nodejs\u4e2d\u4f7f\u7528\uff0c\u4f7f\u7528\u65b9\u5f0f\u6709\u4e00\u70b9\u7c7b\u4f3cajax\u3002\u901a\u8fc7axios.post(&#8220;\/api&#8221;,{param:&#8221;param&#8221;})\u7684\u65b9\u5f0f\u5411\u540e\u53f0\u53d1\u8d77http\u8bf7\u6c42\u3002<\/li>\n<li>\u540e\u53f0\u7684flask\u8fd0\u884c\u8d77\u6765\u4e4b\u540e\uff0c\u901a\u8fc7\u88c5\u9970\u5708route.\u914d\u7f6e\u8def\u7531@app.route(&#8216;\/api&#8217;,methods=[&#8220;GET&#8221;,&#8221;POST&#8221;]) \u6765\u5bf9\u5e94\u524d\u53f0http\u8bf7\u6c42\u7684url\uff0c\u5982\u679c\u6ca1\u6709\u5bf9\u5e94\u7684url\u4f1a\u8fd4\u56de404\u3002\u5982\u679c\u627e\u5230\u5bf9\u5e94\u7684\u8def\u7531\uff0c\u5219\u4f1a\u8fdb\u5165\u76f8\u5e94\u7684\u65b9\u6cd5\uff0c\u8fdb\u884c\u8fd0\u7b97\uff0c\u5b8c\u6210\u8fd0\u7b97\u4e4b\u540e\uff0c\u53ef\u4ee5\u7528json.dumps\u628a\u6570\u636e\u4f5c\u4e3ajson\u8fd4\u56de\u3002<\/li>\n<li>axios\u524d\u53f0\u7684response\u6536\u5230\u540e\uff0c\u901a\u8fc7response.data\u83b7\u5f97\u8fd4\u56de\u7684json\uff0c\u7136\u540e\u53ef\u4ee5\u628a\u76f8\u5e94\u7684\u503c\u8fdb\u884c\u53d8\u66f4<\/li>\n<\/ol>\n<h1><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/h1>\n<ul>\n<li><strong>\u524d\u7aefvue\u5173\u952e\u4ee3\u7801\u4e4b index.html<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:js decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;meta charset=\"utf-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"&gt;\n  &lt;title&gt;vueapp01&lt;\/title&gt;\n  &lt;script src=\"https:\/\/cdn.staticfile.org\/vue\/2.4.2\/vue.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;style&gt;\n  .class1{\n    background: #444;\n    color: #eee;\n  }\n&lt;\/style&gt;\n&lt;body&gt;\n  &lt;div id=\"app3\"&gt;&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<ul>\n<li><strong>\u524d\u7aefvue\u5173\u952e\u4ee3\u7801\u4e4bmain.js<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:js decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">\/\/ The Vue build version to load with the `import` command\n\/\/ (runtime-only or standalone) has been set in webpack.base.conf with an alias.\nimport Vue from 'vue'\nimport App from '.\/App'\nimport Lzw from '.\/components\/Lzw'\nimport router from '.\/router'\nimport axios from 'axios'\nimport BootstrapVue from 'bootstrap-vue'\nimport 'bootstrap\/dist\/css\/bootstrap.css'\nimport 'bootstrap-vue\/dist\/bootstrap-vue.css'\nVue.use(BootstrapVue)\nVue.config.productionTip = false\n\/* eslint-disable no-new *\/\nnew Vue({\n  el: '#app3',\n  router,\n  axios,\n  components: { Lzw },\n  template: '&lt;Lzw\/&gt;'\n})<\/code><\/pre>\n<ul>\n<li><strong>\u524d\u7aefvue\u5173\u952e\u4ee3\u7801\u4e4bLzw.vue<\/strong><\/li>\n<\/ul>\n<p>\u4f7f\u7528axios\u9700\u8981\u5148\u5b89\u88c5axios\u5e93\u548cHTTP2\u5e93<\/p>\n<blockquote><p>npm install &#8211;save axios<br \/>\nnpm install &#8211;save http2<\/p><\/blockquote>\n<pre class=\"lang:js decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;template&gt;\n  &lt;div class=\"hello\"&gt;\n    &lt;h1&gt;{{ msg }}&lt;\/h1&gt;\n    &lt;div id=\"example-3\"&gt;\n      &lt;input type=\"checkbox\" id=\"jack\" value=\"\u767d\u91d1\u4f1a\u5458\" v-model=\"checkedNames\"&gt;\n      &lt;label for=\"jack\"&gt;\u767d\u91d1\u4f1a\u5458&lt;\/label&gt;\n      &lt;input type=\"checkbox\" id=\"john\" value=\"\u9ec4\u91d1\u4f1a\u5458\" v-model=\"checkedNames\"&gt;\n      &lt;label for=\"john\"&gt;\u9ec4\u91d1\u4f1a\u5458&lt;\/label&gt;\n      &lt;input type=\"checkbox\" id=\"mike\" value=\"\u738b\u8005\u4f1a\u5458\" v-model=\"checkedNames\"&gt;\n      &lt;label for=\"mike\"&gt;\u738b\u8005\u4f1a\u5458&lt;\/label&gt;\n      &lt;br&gt;\n      &lt;strong&gt;\u9009\u62e9\u4f1a\u5458\u79cd\u7c7b: {{ checkedNames }}&lt;\/strong&gt;\n    &lt;\/div&gt;\n    &lt;div id=\"app2\"&gt;\n      &lt;input v-model=\"username\" placeholder=\"\u7528\u6237\u540d\"&gt;\n      &lt;p&gt;\u7528\u6237\u540d\u662f: {{ username }}&lt;\/p&gt;\n      &lt;input type=\"password\" v-model=\"password\" placeholder=\"\u5bc6\u7801\"&gt;\n      &lt;p&gt;\u5bc6\u7801\u662f: {{ password }}&lt;\/p&gt;\n      &lt;button class=\"btn btn-large btn-primary\" v-on:click=\"login\"&gt;\u5411\u540e\u53f0\u53d1\u9001post\u8bf7\u6c42\uff0c\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u53d8\u66f4\u7528\u6237ID&lt;\/button&gt;\n      &lt;p&gt;\u7528\u6237ID\u662f: {{ id }}&lt;\/p&gt;\n      &lt;button class=\"btn btn-large btn-primary\" v-on:click=\"getmsg\"&gt;\u5411\u540e\u53f0\u53d1\u9001get\u8bf7\u6c42\uff0c\u628a\u7528\u6237ID\u53d8\u62100&lt;\/button&gt;\n    &lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n&lt;script&gt;\nimport axios from \"axios\";\nexport default {\n  name: \"hello\",\n  data() {\n    return {\n      msg: \"\u6b22\u8fce\u6765\u5230\u6d4b\u8bd5\u5f00\u53d1\u7b14\u8bb0\uff01\",\n      checkedNames: [],\n      username: \"\",\n      password: \"\",\n      id: \"\u5bc6\u7801\u53cd\u8f6c+\u7528\u6237\u540d\u53cd\u8f6c\"\n    };\n  },\n  methods: {\n    login() {\n      var that = this;\n      \/\/ \u5bf9\u5e94 Python \u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u8fd9\u91cc\u7684\u5730\u5740\u586b\u5199\u4e0b\u9762\u670d\u52a1\u5668\u8fd0\u884c\u7684\u5730\u5740\uff0c\u672c\u5730\u5219\u4e3a127.0.0.1\uff0c\u5916\u7f51\u5219\u4e3a your_ip_address\n      const path = \"http:\/\/127.0.0.1:5000\/getMsg\";\n      axios\n        .post(path, { username: this.username, password: this.password })\n        .then(response =&gt; {\n          this.id = response.data.userid;\n        });\n    },\n    getmsg() {\n      var that = this;\n      \/\/ \u5bf9\u5e94 Python \u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u8fd9\u91cc\u7684\u5730\u5740\u586b\u5199\u4e0b\u9762\u670d\u52a1\u5668\u8fd0\u884c\u7684\u5730\u5740\uff0c\u672c\u5730\u5219\u4e3a127.0.0.1\uff0c\u5916\u7f51\u5219\u4e3a your_ip_address\n      const path = \"http:\/\/127.0.0.1:5000\/getMsg\";\n      \/\/ \u52a1\u5fc5\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837this.id\u80fd\u76f4\u63a5\u5bf9\u4e0a\uff0c\u4e0d\u7136\u4f1a\u62a5\u9519\u63d0\u793aid\u6ca1\u627e\u5230\n      axios\n        .get(path, { username: this.username, password: this.password })\n        .then(response =&gt; {\n          this.id = response.data.userid;\n        });\n    }\n  }\n};\n&lt;\/script&gt;<\/code><\/pre>\n<ul>\n<li><strong>\u540e\u7aefflask\u5173\u952e\u4ee3\u7801main.py<\/strong><\/li>\n<\/ul>\n<p>flask\u8981\u907f\u514d\u8de8\u57df\u95ee\u9898\u3002\u9700\u8981\u5b89\u88c5Flask\u5e93\u548cFalsk-Cors\u5e93<\/p>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">from flask import Flask, url_for,request\nfrom flask_cors import *\nimport json\napp = Flask(__name__)\n# \u8fd9\u53e5\u8bdd\u89e3\u51b3\u8de8\u57df\u95ee\u9898\nCORS(app, supports_credentials=True)\n@app.route('\/getMsg',methods=[\"GET\",\"POST\"])\ndef getMsg():\n    if request.method == 'POST':\n        username = request.json['username']\n        password= request.json['password']\n        # \u5047\u5b9a\u7528\u6237id\u662f\u5bc6\u7801\u53cd\u8f6c+\u7528\u6237\u540d\u53cd\u8f6c\u5f97\u51fa\u6765\u7684\n        datat = {\n            \"userid\": username[::-1]+password[::-1],\n        }\n        return json.dumps(datat)\n    elif request.method == 'GET':\n        datat = {\n            \"userid\": 0,\n        }\n        return json.dumps(datat)\nif __name__ == '__main__':\n    app.debug = True\n    app.run()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u673a \u524d\u6bb5\u65f6\u95f4\u51fa\u53bb\u9762\u8bd5\uff0c\u9047\u5230\u4e86\u597d\u51e0\u6b21\u5bf9\u65b9\u9762\u8bd5\u5b98\u95ee\u8fd9\u6837\u7684\u95ee\u9898\uff0c&#8221;restful\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u524d\u540e\u7aef\u600e\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":922,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions\/922"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/media\/395"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}