解决跨域的两种办法 / 网络研习社#22 by lemooljiang

View this thread on steempeak.com
· @lemooljiang ·
$1.88
解决跨域的两种办法 / 网络研习社#22
![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
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id74,841,964
authorlemooljiang
permlink74ca1h-22
categorycn
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"}
created2019-05-16 12:30:51
last_update2019-05-16 12:30:51
depth0
children3
net_rshares3,521,138,361,626
last_payout2019-05-23 12:30:51
cashout_time1969-12-31 23:59:59
total_payout_value1.461 SBD
curator_payout_value0.414 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,577
author_reputation263,700,595,848,611
root_title"解决跨域的两种办法 / 网络研习社#22"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (35)
@adenijiadeshina ·
Probably this is a language I need to learn
properties (22)
post_id74,842,217
authoradenijiadeshina
permlinkre-lemooljiang-74ca1h-22-20190516t123521241z
categorycn
json_metadata{"community":"busy","app":"busy\/2.5.6","format":"markdown","tags":["cn"],"users":[],"links":[]}
created2019-05-16 12:35:21
last_update2019-05-16 12:35:21
depth1
children0
net_rshares0
last_payout2019-05-23 12:35:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length43
author_reputation38,213,975,622,361
root_title"解决跨域的两种办法 / 网络研习社#22"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@surpassinggoogle ·
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
properties (22)
post_id76,632,268
authorsurpassinggoogle
permlinkptbpi2
categorycn
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"}
created2019-06-19 01:58:51
last_update2019-06-19 01:58:51
depth1
children0
net_rshares0
last_payout2019-06-26 01:58:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length654
author_reputation508,940,095,151,809
root_title"解决跨域的两种办法 / 网络研习社#22"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@surpassinggoogle ·
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
properties (22)
post_id76,632,297
authorsurpassinggoogle
permlinkptbpjj
categorycn
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"}
created2019-06-19 01:59:45
last_update2019-06-19 01:59:45
depth1
children0
net_rshares0
last_payout2019-06-26 01:59:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length654
author_reputation508,940,095,151,809
root_title"解决跨域的两种办法 / 网络研习社#22"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000