Python PUT请求不适用于web2py?

Python PUT请求不适用于web2py?,python,web2py,Python,Web2py,我们在web2py上的restfull web服务中的跨源资源共享CORS实现方面遇到了一些问题 我们尝试在web2py的服务器端实现CORS,如下所示: 我们在models/0.py中添加了以下内容,以便在控制器中的实际restfull api处理程序之前更新响应头 =============================== if request.env.http_origin: response.headers['Access-Control-Allow-Origin'] = r

我们在web2py上的restfull web服务中的跨源资源共享CORS实现方面遇到了一些问题

我们尝试在web2py的服务器端实现CORS,如下所示:

我们在models/0.py中添加了以下内容,以便在控制器中的实际restfull api处理程序之前更新响应头

===============================

if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
    response.headers['Access-Control-Allow-Origin'] = "*"
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400

    if request.env.request_method == 'OPTIONS':
        if request.env.http_access_control_request_method:
            print request.env.http_access_control_request_method
            response.headers['Access-Control-Allow-Methods'] = request.env.http_access_control_request_method
            if request.env.http_access_control_request_headers:
                response.headers['Access-Control-Allow-Headers'] = request.env.http_access_control_request_headers
==========================

if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
    response.headers['Access-Control-Allow-Origin'] = "*"
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400

    if request.env.request_method == 'OPTIONS':
        if request.env.http_access_control_request_method:
            print request.env.http_access_control_request_method
            response.headers['Access-Control-Allow-Methods'] = request.env.http_access_control_request_method
            if request.env.http_access_control_request_headers:
                response.headers['Access-Control-Allow-Headers'] = request.env.http_access_control_request_headers
RESTful POST&GET现在正在工作 但PUT和DELETE并不是因为飞行前http选项请求被web2py作为400错误请求拒绝

例如,当从本地网页使用ajax调用调用restful web服务时, 我们在NetBeans日志中得到以下错误消息

加载资源失败:服务器响应状态为400 错误请求10:46:36:182 |错误,网络位于 127.0.0.1:8000/test/default/api/entries/2.json未能加载资源:不允许原始localhost:8383 访问控制允许源。10:46:36:183 |错误,网络位于 127.0.0.1:8000/test/default/api/entries/2.json XMLHttpRequest无法加载127.0.0.1:8000/test/default/api/entries/2.json。起源 访问控制允许源不允许localhost:8383。 10:46:36:183 |错误,www/page/test.html上的javascript


您可以添加以下行:


response[Access Control Allow Methods]=POST、GET、OPTIONS

这是一个非常老的问题,但我刚刚设法解决了完全相同的问题。在我的案例中,问题在于控制器;在执行任何操作之前,我必须添加以下包装:

def CORS(f):
    """
    Enables CORS for any action
    """
    def wrapper(*args, **kwds):
        if request.env.http_origin and request.env.request_method == 'OPTIONS':
            response.view = 'generic.json'
            return dict()
        return f(*args, **kwds)
    return wrapper
然后在你的控制器里写

@CORS
def whatever():
    do_stuff
    return dict(stuff)