๐ฅ ๋ถํธ์คํธ๋ฉ๊ณผ CDN
๐ฅ Vue์ CDN
๐ฅ REST API method ์ ์ํ๊ธฐ
<script src>
๊ฐ Vue๋ฅผ CDN ๋ฐฉ์์ผ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ์ด๊ณ , ๋๋ฒ์งธ <script src>
๋ axios ๋ฐฉ์์ผ๋ก ํต์ ํ๊ธฐ ์ํด ์ถ๊ฐ๋ ๊ฒโ๐ป html์์ Vue๋ฅผ CDN์ผ๋ก ์ฐ๊ฒฐ
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
v-on:click="axios_test"
: ํด๋ฆญ event๊ฐ ๋ฐ์ํ๋ฉด "axios_text"๋ฅผ ์คํโ๐ป html์์๋ฅผ Vue๋ก ์ ์ดํ๋ ๋ฐฉ๋ฒ :
<div id=""></div>
<div id="test_vue"> <center> <button type="button" class="btn btn-primary" v-on:click="axios_test"> GET </button> <button type="button" class="btn btn-secondary">POST</button> <button type="button" class="btn btn-success">PUT</button> <button type="button" class="btn btn-danger">DELETE</button> </center> </div>
โ๐ป ๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> const app = new Vue({ el: "#test_vue", methods: { axios_test() { axios("http://localhost:8082/test", { method: "get", }) .then((response) => { console.log(response.data); }) .catch((error) => { console.log(error); }); }, }, }); </script>
<script></script>
์์ ์ฝ๋์์ ์คํ๋๋ HTTP request๋ ๋ค๋ฅธ ์๋ฒ๋ก ์์ฒญํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด CROS์โ๐ป python
from flask import Flask, request, make_response, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route("/test", methods=['GET']) def test(): return make_response(jsonify(success=True), 200) if __name__ == '__main__': app.run(host="0.0.0.0", port="8082")
โ๐ป 4๊ฐ์ง ๋ฒํผ ์์ฑ ํ Vue๋ก click event ๊ฑธ์ด์ฃผ๊ธฐ
<body> <div id="app"> <br /> <center> <button type="button" class="btn btn-primary" v-on:click="test_get"> GET </button> <button type="button" class="btn btn-secondary" v-on:click="test_post"> POST </button> <button type="button" class="btn btn-success" v-on:click="test_put"> PUT </button> <button type="button" class="btn btn-danger" v-on:click="test_delete"> DELETE </button> </center> </div> </body>
โ๐ป ๊ฐ ๋ฒํผ๋ง๋ค click ๋ฐ์ ์, ์๋ํ๋ ๊ฐ์ฒด ์์ฑ
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> const app = new Vue({ el: "#app", methods: { test_get: () => { axios("http://localhost:8082/test", { method: "get", params: { email: "test@test.com", }, }) .then((response) => { console.log(response.data["status"]); }) .catch((error) => { console.log(error); }); }, test_post: () => { axios("http://localhost:8082/test", { method: "post", data: { email: "test@test.com", }, }) .then((response) => { console.log(response.data["status"]); }) .catch((error) => { console.log(error); }); }, test_put: () => { axios("http://localhost:8082/test", { method: "put", params: { email: "test@test.com", }, }) .then((response) => { console.log(response.data["status"]); }) .catch((error) => { console.log(error); }); }, test_delete: () => { axios("http://localhost:8082/test", { method: "delete", params: { email: "test@test.com", }, }) .then((response) => { console.log(response.data["status"]); }) .catch((error) => { console.log(error); }); }, }, }); </script>
@app.route("/test", methods=['GET', 'POST', 'PUT', 'DELETE'])
: route ๊ฒฝ๋ก(/test์)์ ์ง์ํ๋ methods๋ฅผ ์ ํํ ์ ์์(methods๋ฅผ ๋ณ๋๋ก ์ ์ด์ฃผ์ง ์์ผ๋ฉด get๋ฅผ ์ง์) โ๐ป python
from flask import Flask, request, make_response, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app) # CORS์ app ๊ฐ์ฒด๋ฅผ ๋ฃ์ด์ค # method ๋ฐฉ์๋ณ ์ถ์ถ ๋ฐฉ๋ฒ(post๋ง ๋ค๋ฆ) @app.route("/test", methods=['GET', 'POST', 'PUT', 'DELETE']) def test(): if request.method == 'POST': print('POST') data = request.get_json() print(data) print(data['email']) if request.method == 'GET': print('GET') user = request.args.get('email') print(user) if request.method == 'PUT': print('PUT') user = request.args.get('email') print(user) if request.method == 'DELETE': print('DELETE') user = request.args.get('email') print(user) return make_response(jsonify({'status': True}), 200) if __name__ == '__main__': app.run(host="0.0.0.0", port="8082")