這篇文章主要介紹了關於python的bottle框架跨域請求報錯問題的處理方法,需要的朋友可以參考下
在用python的bottle框架開發時,前端使用ajax跨域存取時,js#程式碼老是進入不了success,而是進入了error,而回傳的狀態卻是200。 url直接在瀏覽器存取也是正常的,瀏覽器按F12後會發現下面這個錯誤提示
XMLHttpRequest cannot load http://192.168.0.118:8081/get_mobile_number/? id=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
#透過搜尋引擎查詢錯誤,會發現幾乎找到的答案都說是跨域問題,只需要在主文件的程式碼中添加下面就可以了,國外的網站好多解決方案都是這樣說明
@hook('after_request') def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*'
而事實上是按找出來的解決方法添加後還是出現錯誤,查看瀏覽器輸出的http頭並沒有看到我們剛剛增加的Access-Control-Allow-Origin:*,如下圖:
通過DEBUG,進入bottle的源碼中查看
這個問題我測試過在python2與python3對應的bottle框架中都存在這種問題,我們將它改為:
class HTTPResponse(Response, BottleException): def init(self, body='', status=None, headers=None, **more_headers): super(HTTPResponse, self).init(body, status, headers, **more_headers) def apply(self, response): response._status_code = self._status_code response._status_line = self._status_line if self._headers: if response._headers: response._headers.update(self._headers) else: response._headers = self._headers response._cookies = self._cookies response.body = self.body
# 再運行程式碼就可以看見ajax程式碼正常了
以上是圖文詳解python的bottle框架跨域請求錯誤問題的處理方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。