【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': '' ,
- '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】跨域服务及访问的更多相关文章
- react axios 跨域访问一个或多个域名
1.react + axios 跨域访问一个域名 配置非常简单,只需要在当前的 package.json 文件里面配置: "proxy":"http://iot-demo ...
- 直接用postman测试api ,服务器端没提供跨域也可以访问。
1. 直接用postman测试api ,服务器端没提供跨域也可以访问. 但是,如果用本地的 sever 搭的server, 然后去访问api的话,浏览器会提示 跨域错误.
- flask跨域请求三行代码搞定
flask跨域请求三行代码就可以搞定.但是请注意几点: 第一:只能返回json格式数据,比如list.ndarray等都不可以 第二:返回的对象必须是是字符串.元组.响应实例或WSGI可调用. pyt ...
- angularjs flask跨域问题 XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin'
场景,我要来我的server(A)上用api来访问另一个server(B)的问题,如果直接在A上调用B的api,那么就会出现XMLHttpRequest cannot load. No 'Access ...
- flask 跨域请求
Flask中,跨域请求主要有两种方式: 1.在响应头信息中添加允许跨域 如下,使用装饰器app.after_request(我这里的web是定义的蓝图),这样在每次请求后,加入header 2.使用第 ...
- Webpack4 学习笔记七 跨域服务代理
webpack 小插件使用 webpack 监听文件变化配置 webpack 处理跨域问题 Webpack 小插件使用 clean-webpack-plugin: 用于在生成之前删除生成文件夹的Web ...
- tornado django flask 跨域解决办法(cors)
XMLHttpRequest cannot load http://www.baidu.com. No 'Access-Control-Allow-Origin' header is present ...
- WCF 自托管、无配置文件实现jsonp(跨域)的访问
以下内容基于WCF4.0,本文将对比讨论配置文件方案和无配置文件方案的实现方式. WCF4.0加入了对RESTFU和标准终结点的支持,这为实现跨域提供了简单的方式. 一.有配置文件的情况: 首先我们先 ...
- Chrome 浏览器跨域和安全访问问题 使用 chrome的命令行标记:disable-web-security 参数联调线上数据
做前端的,用Ajax获取数据,是常有的事情,同域下自然没问题了,如果是不同域获取数据,浏览器就有个同源策略的限制. 如图: Origin * is not allowed by Access-Cont ...
随机推荐
- 谈谈canvas的性能优化(主要讲缓存问题)
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! canvas玩多了后,就会自动的要开始考虑性能问题了.怎么优化canvas的动画呢? [使用缓存] 使用缓存也就是用离屏canvas进行预 ...
- BZOJ2702 : 金融风暴
求出离每个点最近的关键点,然后用二维ST表回答正方形最大值. 将关键点分为上下两部分,以上为例: 从上到下依次考虑每一行,记录每一列往上最近的关键点的距离,那么最优决策具有单调性,可以分治求解. 时间 ...
- Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
最近的一个问题,电脑关机的时候发现,只有“睡眠”.“更新并重启”.“更新并关机” 内心很是煎熬.... 尝试了N种方式,然后总结如下: 第一种方式:(表示自己window的系统用着挺好,力荐) 1.打 ...
- Creator 插件商店:高品质插件
资源处理类 资源引用查询 功能:将指定资源拖到目标资源框内并列出所有需要用到该资源的场景以及所在节点 点评:检查一下是否有冗余资源混进来了,尽量减少包体积呀. TexturePacker 碎图提取 功 ...
- Java集合框架(比较啰嗦)
阅读目录 概念与作用 集合框架的体系结构 Collection接口和List接口简介 Map和HashMap简介 集合工具类:Collections 小结 概念与作用 集合概念 现实生活中:很多事物凑 ...
- ArrayList源码分析和实例应用
1.ArrayList介绍 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAcces ...
- Python核心编程(第二版)正则表达式练习题解
15-1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” from re import match word = raw_input('inpu ...
- Linux学习-实验楼(1)
1.Shell:命令解释器 2.Linux技能树 3.X11:X窗口系统,其本身只是工具包及架构协议,Xorg是x架构规范的一个实现体,是实现了x协议规范的一个提供图形界面的服务器 4.终端本质上是对 ...
- ABAP表生成Java实体Bean
项目中需要将HR模块中的表数据同步到Java系统中,向外围系统提供分发与查询服务,涉及到的表有两百多张,字段好几千上万个,如果手工一张张这些ABAP表在Java系统数据库中创建一遍的话,工作量将非常大 ...
- centos7下使用mysql离线安装包安装mysql5.7
服务器环境: centos7 x64 需要安装mysql5.7+ 一.卸载CentOS7系统自带mariadb # 查看系统自带的Mariadb [root@CDH-141 ~]# rpm -qa|g ...