網頁內請求一般不允許跨域,并且是根據二級域名級別限制(網頁當前域名)
1.使用jsonp :jsonp只允許get而不支持post等
2.修改服務器返回response的header頭(Access-Control-Allow-Origin),但為了安全也最好加個判斷,只允許自己允許的少數域名可以跨域
"""Write json to client"""
set_header('Content-type', 'application/json; charset=UTF-8')
# 獲取請求headers頭Origin
http_origin = self.request.headers['Origin'] if 'Origin' in self.request.headers else '*'
# 允許固定域名跨域
if http_origin in ['http://app.baidu.com', 'http://api.baidu.com']:
? ? ? ? set_header("Access-Control-Allow-Origin", http_origin)
? ? ? ? set_header("Access-Control-Allow-Credentials", "true")
? ? ? ? set_header("Access-Control-Allow-Methods", "*")
? ? ? ? set_header("Access-Control-Allow-Headers", "Content-Type,Accept,Authorization")
? ? ? ? set_header("Access-Control-Max-Age", "86400")