【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 ...
随机推荐
- Node回调&事件
Node回调&事件 Node.js 异步编程的直接体现就是回调,异步编程依托于回调来实现 简介 回调函数在任务完成后会被调用,node的所有API都支持回调函数,回调函数提高了Node的性能, ...
- c++ 引用犯的一个错
void cal(int * &a) { ................ } int main() { int x=1; int *p=x; cal( &x );//出错 cal( ...
- 关于django的部分
新建Django工程 django-admin.exe startproject mysite 新建Django工程 新建static(静态模板文件存储路径) setting中配置static路径 S ...
- idea其他人把jar更新之后更新不到
昨天下午开始就发现这个问题,其他同事把jar更新了之后,我一直获取不到更新之后的内容.尝试了很多方法,删除具体的更新不到的jar,一直不停的mvn clean install -U -Dmaven.t ...
- web的几种返回顶部
回到顶部backtoTop 滚动回到顶部 jquery/js效果还不错!支持现代浏览器包括 ie6.position: absolute;和fixed.两种方法! 一,jQuery方法的backtoT ...
- Deepin 15.4 破解安装 SecureFX-7.3 失败
参考先前的文章(Ubuntu 14 安装并破解SSH工具 SecureCRT),破解安装 SecureCRT-7.3 成功,但是破解安装 SecureFX-7.3 时就一直报错: Insuffici ...
- CoordinatorLayout使用详解: 打造折叠悬浮效果
1.简介 CoordinatorLayout遵循Material 风格,包含在 support Library中,结合AppbarLayout, CollapsingToolbarLayout等 可 ...
- Chart:Grafana
ylbtech-Chart:Grafana 1.返回顶部 1-1. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https://grafana.com/ 1. http: ...
- .NET上传大文件时提示Maximum request length exceeded错误的解决方法
使用IIS托管应用程序时,当我们需要上传大文件(4MB以上)时,应用程序会提示Maximum request length exceeded的错误信息.该错误信息的翻译:超过最大请求长度. 解决方法: ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...