zoukankan      html  css  js  c++  java
  • 【React + flask】跨域服务及访问

    Flask

    from flask import Flask , request
    from flask_cors import *
    import flask
    import json
    import pickle
    app = Flask(__name__)
    CORS(app, resources=r'/*')
    
    headers = {
        'Cache-Control' : 'no-cache, no-store, must-revalidate',
        'Pragma' : 'no-cache' ,
        'Expires': '0' ,
        'Access-Control-Allow-Origin' : 'http://localhost:3000',
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token'
    }
    
    
    
    @app.route('/api/timers', methods=["GET"])
    def get_timers(*args):
        with open('./data.json','r+') as f:
            timers_json = json.load(f)
        rsp = flask.Response(json.dumps(timers_json))
        rsp.headers = headers
        rsp.headers['Cache-Control'] = 'no-cache'
        return rsp

    React

    window.client = (function () {
      function getTimers(success) {
        return fetch('http://localhost:3001/api/timers', {
          headers: {
            'Accept': 'application/json',
          },
        }).then(checkStatus)
          .then(parseJSON)
          .then(success);
      }
    
      function createTimer(data) {
        return fetch('http://localhost:3001/api/timers', {
          method: 'post',
          body: JSON.stringify(data),
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
        }).then(checkStatus);
      }
    
      function updateTimer(data) {
        return fetch('http://localhost:3001/api/timers', {
          method: 'put',
          body: JSON.stringify(data),
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
        }).then(checkStatus);
      }
    
      function deleteTimer(data) {
        return fetch('http://localhost:3001/api/timers', {
          method: 'delete',
          body: JSON.stringify(data),
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
        }).then(checkStatus);
      }
    
      function startTimer(data) {
        console.log("startTimer")
        return fetch('http://localhost:3001/api/timers/start', {
          method: 'post',
          mode:'cors',
          body: JSON.stringify(data),
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            //'Content-Type':'application/x-www-form-urlencoded'
          },
        }).then(checkStatus);
      }
    
      function stopTimer(data) {
        return fetch('http://localhost:3001/api/timers/stop', {
          method: 'post',
          body: JSON.stringify(data),
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
        }).then(checkStatus);
      }
    
      function checkStatus(response) {
        if (response.status >= 200 && response.status < 300) {
          return response;
        } else {
          const error = new Error(`HTTP Error ${response.statusText}`);
          error.status = response.statusText;
          error.response = response;
          console.log(error);
          throw error;
        }
      }
    
      function parseJSON(response) {
        return response.json();
      }
    
      return {
        getTimers,
        createTimer,
        updateTimer,
        startTimer,
        stopTimer,
        deleteTimer,
      };
    }());
  • 相关阅读:
    我说AOP(面向切面编程)--藏在苹果里的五角星
    mysql workbench 一个‘愚蠢’的设计
    .Net MVC Json 日期格式
    es6 import
    asp.net mvc 模型绑定太糙淡了
    asp.net mvc 报错 CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
    撸代码时到底用var好还是强类型变量好
    iphone5 从ios7升级到最新9.2
    修复win7 只有IE64 能上网 其他浏览器及应用都无法联网
    使用Teleri 导出实体类数组到Excel
  • 原文地址:https://www.cnblogs.com/colipso/p/9376935.html
Copyright © 2011-2022 走看看