web服務端設置返回允許跨域問題

網頁內請求一般不允許跨域,并且是根據二級域名級別限制(網頁當前域名)

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")

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容