![vue rest.jpg](https://cdn.steemitimages.com/DQmcMHHPMyBXAF7bjKBnkgyR9ZBafndMKUjYRneidq7bawe/vue%20rest.jpg) 在前一节中分享了前后端设计的理念和方法。这个方法遇到的第一个问题就是跨域的问题。访问的端口不同,域名不同都会跨域。所以,只要是前后端分离就会遇到这个问题,当然,这也是服务器的一些防范机制。在服务器端解决跨域有两个办法,一个自己写,一个是用现成的。 ## 一、创建cors.py解决跨域 ```py class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_response super(MiddlewareMixin, self).__init__() def __call__(self, request): response = None if hasattr(self, 'process_request'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self, 'process_response'): response = self.process_response(request, response) return response class CORSMiddleware(MiddlewareMixin): def process_response(self,request,response): # 添加响应头 # 允许你的域名来获取我的数据 response['Access-Control-Allow-Origin'] = "*" # 允许你携带Content-Type请求头 response['Access-Control-Allow-Headers'] = "Content-Type" # 允许你发送DELETE,PUT response['Access-Control-Allow-Methods'] = "DELETE,PUT,GET,POST" if request.method == "OPTIONS": response['Access-Control-Allow-Headers'] = "Content-Type" response['Access-Control-Allow-Methods'] = "PUT,DELETE" return response 在settings.py中添加中间件 MIDDLEWARE = [ 'app01.cors.CORSMiddleware', // 要放在最上面 ... ] ``` ## 二、用django-cors-headers来解决跨域 [django-cors-headers](https://github.com/ottoyiu/django-cors-headers) ,分四步: ``` 1. pip install django-cors-headers #settings.py 2. INSTALLED_APPS = ( ... 'corsheaders', ) 3. MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10 'corsheaders.middleware.CorsMiddleware', ... ] 4. CORS_ORIGIN_ALLOW_ALL = True ``` **** 网络研习社系列文章: * [微信小程序开发初体验 / 网络研习社#1]( https://steemit.com/cn/@lemooljiang/61qx8h-1) * [新技能:小程序空间当图床! / 网络研习社#2](https://steemit.com/cn/@lemooljiang/gjekw-2) * [小程序云开发中数据的传递形式 / 网络研习社#3](https://steemit.com/cn/@lemooljiang/6a8ukt-3) * [如何突破coreldraw的网络限制 / 网络研习社#4](https://steemit.com/cn/@lemooljiang/coreldraw-4) * [我师网小程序初发布,大家多多指教 / 网络研习社#5](https://steemit.com/cn/@lemooljiang/6rr2ug-5) * [用github 做文件目录 / 网络研习社#6](https://steemit.com/cn/@lemooljiang/github-6) * [LNMP环境一键安装(一) / 网络研习社#7](https://steemit.com/cn/@lemooljiang/lnmp-7) * [LNMP环境自定义安装(二) / 网络研习社#8](https://steemit.com/cn/@lemooljiang/lnmp-8) * [利用github做免费博客 / 网络研习社#9](https://steemit.com/cn/@lemooljiang/github-9) * [Nodejs,打开服务器黑匣子 / 网络研习社#10](https://steemit.com/cn/@lemooljiang/nodejs-10) * [一入前端深似海,聊聊vue.js / 网络研习社#11]( https://steemit.com/cn/@lemooljiang/vue-js-11) * [我们想做的,vue都帮我们做好了 / 网络研习社#12]( https://steemit.com/cn/@lemooljiang/vue-11) * [vue和小程序一家亲 / 网络研习社#13]( https://steemit.com/cn/@lemooljiang/vue-13) * [vue的组件面面观 / 网络研习社#14]( https://steemit.com/cn/@lemooljiang/vue-14) * [vue-router路由的参数和设计 / 网络研习社#15]( https://steemit.com/cn/@lemooljiang/vue-router-15) * [webpack前端神器 / 网络研习社#16]( https://steemit.com/cn/@lemooljiang/webpack-16) * [用FileZilla作FTP文件服务器 / 网络研习社#17](https://steemit.com/cn/@lemooljiang/filezilla-ftp-17) * [连接FTP文件服务器的方法/ 网络研习社#18](https://steemit.com/cn/@lemooljiang/ftp-18) * [axios请求HTTP和vuex数据管理 / 网络研习社#19](https://steemit.com/cn/@lemooljiang/axios-http-vuex-19) * [YouTube 视频下载利器—Youtube-DLG / 网络研习社#20](https://steemit.com/cn/@lemooljiang/youtube-youtube-dlg-20) * [Vue和django rest framework做前后端分离设计 / 网络研习社#21](https://steemit.com/cn/@lemooljiang/vue-django-rest-framework-21) **** @lemooljiang #network-institute
post_id | 74,841,964 |
---|---|
author | lemooljiang |
permlink | 74ca1h-22 |
category | cn |
json_metadata | {"tags":["cn","network-institute","vue","python","django"],"users":["lemooljiang"],"image":["https:\/\/cdn.steemitimages.com\/DQmcMHHPMyBXAF7bjKBnkgyR9ZBafndMKUjYRneidq7bawe\/vue%20rest.jpg"],"links":["https:\/\/github.com\/ottoyiu\/django-cors-headers","https:\/\/steemit.com\/cn\/@lemooljiang\/61qx8h-1","https:\/\/steemit.com\/cn\/@lemooljiang\/gjekw-2","https:\/\/steemit.com\/cn\/@lemooljiang\/6a8ukt-3","https:\/\/steemit.com\/cn\/@lemooljiang\/coreldraw-4","https:\/\/steemit.com\/cn\/@lemooljiang\/6rr2ug-5","https:\/\/steemit.com\/cn\/@lemooljiang\/github-6","https:\/\/steemit.com\/cn\/@lemooljiang\/lnmp-7","https:\/\/steemit.com\/cn\/@lemooljiang\/lnmp-8","https:\/\/steemit.com\/cn\/@lemooljiang\/github-9","https:\/\/steemit.com\/cn\/@lemooljiang\/nodejs-10","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-js-11","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-11","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-13","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-14","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-router-15","https:\/\/steemit.com\/cn\/@lemooljiang\/webpack-16","https:\/\/steemit.com\/cn\/@lemooljiang\/filezilla-ftp-17","https:\/\/steemit.com\/cn\/@lemooljiang\/ftp-18","https:\/\/steemit.com\/cn\/@lemooljiang\/axios-http-vuex-19","https:\/\/steemit.com\/cn\/@lemooljiang\/youtube-youtube-dlg-20","https:\/\/steemit.com\/cn\/@lemooljiang\/vue-django-rest-framework-21"],"app":"steemit\/0.1","format":"markdown"} |
created | 2019-05-16 12:30:51 |
last_update | 2019-05-16 12:30:51 |
depth | 0 |
children | 3 |
net_rshares | 3,521,138,361,626 |
last_payout | 2019-05-23 12:30:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 1.461 SBD |
curator_payout_value | 0.414 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 3,577 |
author_reputation | 263,700,595,848,611 |
root_title | "解决跨域的两种办法 / 网络研习社#22" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
lemooljiang | 0 | 543,633,892,219 | 100% | ||
ace108 | 0 | 432,188,514,584 | 25% | ||
rivalhw | 0 | 1,980,215,266,378 | 100% | ||
dapeng | 0 | 36,161,772,256 | 100% | ||
bring | 0 | 3,458,656,574 | 100% | ||
laodr | 0 | 170,117,705,937 | 100% | ||
haiyangdeperci | 0 | 7,111,130,614 | 20% | ||
catwomanteresa | 0 | 50,766,960,959 | 40% | ||
liangfengyouren | 0 | 1,819,952,550 | 50% | ||
tvb | 0 | 37,851,533,365 | 80% | ||
skenan | 0 | 18,573,664,366 | 50% | ||
yellowbird | 0 | 24,923,351,970 | 100% | ||
davidke20 | 0 | 3,890,751,829 | 4% | ||
dancingapple | 0 | 12,132,311,723 | 50% | ||
xiaoshancun | 0 | 12,611,341,741 | 100% | ||
hqy | 0 | 8,009,545,673 | 100% | ||
zmm | 0 | 537,584,401 | 100% | ||
lindalex | 0 | 186,581,654 | 20% | ||
taos | 0 | 69,878,475 | 20% | ||
lebin | 0 | 55,395,698,425 | 5% | ||
coindzs | 0 | 6,301,997,360 | 100% | ||
nostalgic1212 | 0 | 17,965,948,511 | 48% | ||
heiduya | 0 | 77,018,770 | 30% | ||
sunai | 0 | 3,936,904,197 | 45% | ||
julian2013 | 0 | 92,076,801,814 | 100% | ||
steeming-hot | 0 | 0 | 0.01% | ||
evelaurie | 0 | 77,223,030 | 30% | ||
yalehazlitt | 0 | 77,070,312 | 30% | ||
eltonwright | 0 | 71,245,308 | 30% | ||
zorapolly | 0 | 71,095,307 | 30% | ||
yettawild | 0 | 71,095,307 | 30% | ||
bobarchibald | 0 | 71,095,904 | 30% | ||
annasandy | 0 | 71,095,891 | 30% | ||
sskiller | 0 | 70,306,691 | 30% | ||
microlhy | 0 | 543,367,531 | 100% |
Probably this is a language I need to learn
post_id | 74,842,217 |
---|---|
author | adenijiadeshina |
permlink | re-lemooljiang-74ca1h-22-20190516t123521241z |
category | cn |
json_metadata | {"community":"busy","app":"busy\/2.5.6","format":"markdown","tags":["cn"],"users":[],"links":[]} |
created | 2019-05-16 12:35:21 |
last_update | 2019-05-16 12:35:21 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-05-23 12:35:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 43 |
author_reputation | 38,213,975,622,361 |
root_title | "解决跨域的两种办法 / 网络研习社#22" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
Hi bro, I have some tasks related to vue and react all on Github and utopian approved. Can you help me with some? I have some steem bounty too and the bounty isn’t big as I am really short on funds but I will love to tip additional steem especially where contributions are timely. I really need help basically. Here is a list: https://steemit.com/utopian-io/@surpassinggoogle/in-dire-need-of-help-are-you-ready-to-make-your-picks-1500-steem-and-20-000-teardrops-shared-among-a-collection-of-development Most of the assigned tasks havent really been taken yet as those who were assigned became unavailable. The projects are steemgigs.org and ulogs.org
post_id | 76,632,268 |
---|---|
author | surpassinggoogle |
permlink | ptbpi2 |
category | cn |
json_metadata | {"tags":["cn"],"links":["https:\/\/steemit.com\/utopian-io\/@surpassinggoogle\/in-dire-need-of-help-are-you-ready-to-make-your-picks-1500-steem-and-20-000-teardrops-shared-among-a-collection-of-development"],"app":"steemit\/0.1"} |
created | 2019-06-19 01:58:51 |
last_update | 2019-06-19 01:58:51 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-06-26 01:58:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 654 |
author_reputation | 508,940,095,151,809 |
root_title | "解决跨域的两种办法 / 网络研习社#22" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
Hi bro, I have some tasks related to vue and react all on Github and utopian approved. Can you help me with some? I have some steem bounty too and the bounty isn’t big as I am really short on funds but I will love to tip additional steem especially where contributions are timely. I really need help basically. Here is a list: https://steemit.com/utopian-io/@surpassinggoogle/in-dire-need-of-help-are-you-ready-to-make-your-picks-1500-steem-and-20-000-teardrops-shared-among-a-collection-of-development Most of the assigned tasks havent really been taken yet as those who were assigned became unavailable. The projects are steemgigs.org and ulogs.org
post_id | 76,632,297 |
---|---|
author | surpassinggoogle |
permlink | ptbpjj |
category | cn |
json_metadata | {"tags":["cn"],"links":["https:\/\/steemit.com\/utopian-io\/@surpassinggoogle\/in-dire-need-of-help-are-you-ready-to-make-your-picks-1500-steem-and-20-000-teardrops-shared-among-a-collection-of-development"],"app":"steemit\/0.1"} |
created | 2019-06-19 01:59:45 |
last_update | 2019-06-19 01:59:45 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-06-26 01:59:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 654 |
author_reputation | 508,940,095,151,809 |
root_title | "解决跨域的两种办法 / 网络研习社#22" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |