Handling of errors reported by the python bottle framework for cross-origin requests, pythonbottle
When using the python bottle framework for development, when the front-end uses ajax for cross-origin access, js Code is always unable to enter success, but error, but the returned status is 200. It is also normal that the url is accessed directly in the browser. After the browser presses F12, the following error message is displayed:
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.
If a search engine query error occurs, you can find that almost all the searched answers are cross-Origin questions. You only need to add the following to the code of the main file. Many solutions for foreign websites are described as follows:
@hook('after_request')def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*'
In fact, an error still occurs after the solution is added. The http header output by the browser does not display the Access-Control-Allow-Origin: * We just added, for example:
Use DEBUG to go to the bottle source code to view
I have tested this problem in the bottle framework corresponding to python2 and python3. We changed it:
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
Run the code to see that the ajax code is normal.
Copyright:
This article was originally published by AllEmpty and published in the blog Park. You are welcome to repost it. You must keep this statement without your consent and provide the original article link clearly on the article page,Otherwise, the right to pursue legal liability is reserved.. If you have any questions, you can use1654937@qq.comThank you very much for contacting me.
Publish the content of this series,MasterYesIt is to learn and make progress together with everyone. If you are interested, you can add Q group: 327360708. Let's discuss it together.
For more information, please refer to blog: http://www.cnblogs.com/EmptyFS/