zoukankan      html  css  js  c++  java
  • axios

    html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="./node_modules/vue/dist/vue.js"></script>
        <!--http://www.axios-js.com/zh-cn/-->
        <script src="./node_modules/axios/dist/axios.js"></script>
    </head>
    <body>
    <div id="app"></div>
    
    <script>
        Vue.prototype.$axios = axios;
        axios.defaults.baseURL = 'http://127.0.0.1:5000';
    
        var App = {
            template: `<div>
                <button @click="sendAjax">发送get</button>
                <button @click="sendAjaxPost">发送post</button>
                {{datas}}
            </div>`,
            data: function () {
                return {
                    msg: '',
                    datas: [],
                }
            },
            methods: {
                sendAjax: function () {
                    this.$axios.get('/')
                        .then(function (response) {
                            console.log(response);
    
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                sendAjaxPost: function () {
                    var params = new URLSearchParams();
                    params.append('name', 'hg');
                    _this = this;//不使用=>时,this在axios中代表window
                    this.$axios.post('/create', params)
                        .then((response) => {
                            console.log(response);
                            console.log(this);//this为window
                            console.log(_this);//_this为Vue
                            // _this.datas = response
                            this.datas = response //推荐使用=>箭头函数
    
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }
        };
    
    
        new Vue({
            el: "#app",
            template: '<App />',
            components: {
                App
            }
        })
    
    </script>
    </body>
    </html>
    View Code

    flask

    from flask import Flask
    from flask import request
    from flask import Response
    
    import json
    # from flask_cors import CORS
    
    app = Flask(__name__)
    
    # CORS(app, supports_credentials=True)
    
    # 默认是get请求
    @app.route("/")
    def index():
        resp = Response("<h2>首页</h2>")
        resp.headers["Access-Control-Allow-Origin"] = "*"
        return resp
    
    
    @app.route("/course")
    def courses():
    
        resp = Response(json.dumps({
            "name": 'alex'
        }))
        resp.headers["Access-Control-Allow-Origin"] = "*"
    
        return resp
    
    
    @app.route("/create", methods=["post", ])
    def create():
        print(request.form.get('name'))
    
        with open("user.json", "r") as f:
            # 将数据反序列化
            data = json.loads(f.read())
    
        data.append({"name": request.form.get('name')})
    
        with open("user.json", "w") as f:
            f.write(json.dumps(data))
    
        resp = Response(json.dumps(data))
    
        resp.headers["Access-Control-Allow-Origin"] = "*"
        # resp.headers["Access-Control-Allow-Methods"] = "*"
        # resp.headers['Access-Control-Allow-Origin'] = '*'
        # resp.headers['Access-Control-Allow-Methods'] = 'GET,POST'
        # resp.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
    
        return resp
    
    
    
    if __name__ == '__main__':
        app.run()
    View Code
  • 相关阅读:
    了解PCI Express的Posted传输与Non-Posted传输
    最强加密算法?AES加解密算法Matlab和Verilog实现
    校招必看硬核干货:IC前端这样学,秒变offer收割机!
    一次压力测试Bug排查-epoll使用避坑指南
    硬核干货 | C++后台开发学习路线
    Web服务器项目详解
    O准备如何苟进复赛圈?华为软挑开挂指南(附赛题预测)
    Linux最大文件句柄(文件描述符)限制和修改
    linux中对EINTR错误的处理
    C/C++实现单向循环链表(尾指针,带头尾节点)
  • 原文地址:https://www.cnblogs.com/hougang/p/11390593.html
Copyright © 2011-2022 走看看