Bottle Py:为jQuery Ajax请求启用CORS

5rgfhyps  于 2023-11-17  发布在  jQuery
关注(0)|答案(4)|浏览(116)

我正在使用Bottle Web Framework上的Web服务的REST风格的API,并希望通过jQuery Ajax调用访问资源。
使用REST客户端,资源接口可以正常工作并正确处理GET,POST,.请求。但是当发送jQuery Ajax POST请求时,产生的OPTIONS preflight请求被简单地拒绝为'405:Method not allowed'。
我尝试在Bottle服务器上启用CORS-如此处所述:http://bottlepy.org/docs/dev/recipes.html#using-the-hooks-plugin但OPTIONS请求从未调用after_request钩子
以下是我的服务器的摘录:

from bottle import Bottle, run, request, response
import simplejson as json

app = Bottle()

@app.hook('after_request')
def enable_cors():
    print "after_request hook"
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

@app.post('/cors')
def lvambience():
    response.headers['Content-Type'] = 'application/json'
    return "[1]"

[...]

字符串
jQuery Ajax调用:

$.ajax({
    type: "POST",
    url: "http://192.168.169.9:8080/cors",
    data: JSON.stringify( data ),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        alert(data);
    },
    failure: function(err) {
        alert(err);
    }
});


服务器只记录405错误:

192.168.169.3 - - [23/Jun/2013 17:10:53] "OPTIONS /cors HTTP/1.1" 405 741


$.post确实可以工作,但是不能发送PUT请求会破坏RESTful服务的目的。那么我如何允许OPTIONS preflight请求被处理呢?

nfg76nw0

nfg76nw01#

安装一个处理程序而不是钩子。
在过去,我有两种互补的方法:装饰器或Bottle插件。我将向您展示这两种方法,您可以决定它们中的一种(或两种)是否适合您的需求。在这两种情况下,一般的想法是:处理程序在将响应发送回客户端之前拦截响应,插入CORS头,然后继续返回响应。

方法一:安装Per-route(Decorator)

当你只想在一些路由上运行处理程序时,这个方法是更可取的。只要装饰你想要它执行的每个路由。下面是一个例子:

import bottle
from bottle import response

# the decorator
def enable_cors(fn):
    def _enable_cors(*args, **kwargs):
        # set CORS headers
        response.headers['Access-Control-Allow-Origin'] = '*'
        response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
        response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

        if bottle.request.method != 'OPTIONS':
            # actual request; reply with the actual response
            return fn(*args, **kwargs)

    return _enable_cors

app = bottle.app()

@app.route('/cors', method=['OPTIONS', 'GET'])
@enable_cors
def lvambience():
    response.headers['Content-type'] = 'application/json'
    return '[1]'

app.run(port=8001)

字符串

方法二:全局安装(瓶插件)

如果你想让处理程序在所有或大部分路由上执行,那么这个方法更好。你只需要define a Bottle plugin一次,Bottle就会在每个路由上自动调用它;不需要在每个路由上指定装饰器。(注意,你可以使用路由的skip参数来避免每个路由上的这个处理程序。)下面是一个与上面的例子相对应的例子:

import bottle
from bottle import response

class EnableCors(object):
    name = 'enable_cors'
    api = 2

    def apply(self, fn, context):
        def _enable_cors(*args, **kwargs):
            # set CORS headers
            response.headers['Access-Control-Allow-Origin'] = '*'
            response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
            response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

            if bottle.request.method != 'OPTIONS':
                # actual request; reply with the actual response
                return fn(*args, **kwargs)

        return _enable_cors

app = bottle.app()

@app.route('/cors', method=['OPTIONS', 'GET'])
def lvambience():
    response.headers['Content-type'] = 'application/json'
    return '[1]'

app.install(EnableCors())

app.run(port=8001)

zqdjd7g9

zqdjd7g92#

这里是对@罗恩.rothman的方法#2的一个小小的改进,用于全局安装CORS处理程序。他的方法要求您指定在您声明的每个路由上接受OPTIONS方法。此解决方案为所有OPTIONS请求安装全局处理程序。

@bottle.route('/<:re:.*>', method='OPTIONS')
def enable_cors_generic_route():
    """
    This route takes priority over all others. So any request with an OPTIONS
    method will be handled by this function.

    See: https://github.com/bottlepy/bottle/issues/402

    NOTE: This means we won't 404 any invalid path that is an OPTIONS request.
    """
    add_cors_headers()

@bottle.hook('after_request')
def enable_cors_after_request_hook():
    """
    This executes after every route. We use it to attach CORS headers when
    applicable.
    """
    add_cors_headers()

def add_cors_headers():
    if SOME_CONDITION:  # You don't have to gate this
        bottle.response.headers['Access-Control-Allow-Origin'] = '*'
        bottle.response.headers['Access-Control-Allow-Methods'] = \
            'GET, POST, PUT, OPTIONS'
        bottle.response.headers['Access-Control-Allow-Headers'] = \
            'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

字符串

esbemjvw

esbemjvw3#

你不应该用这个吗?

response.set_header('Access-Control-Allow-Origin', '*')
response.add_header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS')

字符串

cngwdvgl

cngwdvgl4#

考虑让你的web服务器,而不是瓶子,设置头。
不知道这是否适用于你的情况,但我在过去的项目中已经解决了这个问题,在Apache中为我的Bottle应用程序设置CORS头。它很容易配置,保持我的Python代码漂亮干净,而且效率很高。
信息在from many sources上可用,但如果你使用的是Apache,这里是我的配置的样子(或多或少):

<Location "/cors">
    Header set Access-Control-Allow-Headers "Origin, Content-Type"
    Header set Access-Control-Allow-Methods "POST, GET, OPTIONS"
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Request-Headers "Origin, Content-Type"
</Location>

字符串

相关问题