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