Cross-domain access to Python web development under the "Python" bottle framework

Source: Internet
Author: User

The previous article introduced the Python Application bottle Lightweight framework for Web development, this time introducing cross-domain access issues in the bottle framework.


When the current cross-domain access, the data cannot be obtained from the background, which means that cross-domain access failed.

The solution is as follows:

Define a function code in the program as follows:

#!/usr/bin/python#-*-conding:utf-8-*-from bottle Import * #decoratordef Allow_cross_domain (FN):    def _enable_cors (*args, **kwargs):        #set Cross headers        response.headers[' access-control-allow-origin '] = ' * '        response.headers[' access-control-allow-methods ' = ' get,post,put,options '        allow_headers = ' Referer, Accept, Origin, user-agent '        response.headers[' access-control-allow-headers '] = allow_headers             If Bottle.request.method! = ' OPTIONS ':            # actual request; Reply with the actual response            return fn (*args, **kwargs) 
   return _enable_cors@route ('/helloworld/:yourwords ', methods=[' GET ', ' POST ']) @allow_cross_domain                                              # Add the defined function def hello (yourwords) here: Return ' Hello World '. ' + yourwordsrun (host= ' 0.0.0.0 ', port=8080)


As shown in the code above, the cross-domain access problem can be resolved at each invocation, plus the function for cross-domain.


We hope to help you. Thank you.



Cross-domain access to Python web development under the "Python" bottle framework

Related Article

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.