How to handle cross-origin request errors in the python bottle framework

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.