This article mainly introduces how to handle the cross-origin request error of the python bottle framework, for more information about how to handle cross-origin request errors of the python bottle framework, see the next article.
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.
The above is a detailed description of how to handle cross-origin request errors in the python bottle framework. For more information, see other related articles in the first PHP community!