码迷,mamicode.com
首页 > 其他好文 > 详细

【React + flask】跨域服务及访问

时间:2018-07-27 14:41:24      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:color   load   orm   pre   status   const   body   art   eval   

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,
  };
}());

 

【React + flask】跨域服务及访问

标签:color   load   orm   pre   status   const   body   art   eval   

原文地址:https://www.cnblogs.com/colipso/p/9376935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!