微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

App Engine应用程序中的CORS(python)

我正在尝试在app引擎中的2个应用程序之间执行x-domain请求.一方面,我有我的API,另一方面,我有我的“客户端应用程序”.我一直在读CORS这么多;我想我知道它是如何工作的,问题就出现了:它不起作用.简单的请求有效,但当我尝试执行非简单请求(使用凭据)时问题就出现了.我有这个代码来处理标头并允许CORS:

try:
    _origin = self.request.headers['Origin']
except:
    _origin = "http://myapp"
self.response.headers.add_header("Access-Control-Allow-Origin", _origin)
self.response.headers.add_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.response.headers.add_header("Access-Control-Allow-Credentials", "true")
self.response.headers.add_header("Access-Control-Allow-Headers", "origin, x-requested-with, content-type, accept")
self.response.headers.add_header('Content-Type', 'application/json')
self.response.out.write( json.dumps( _response ) )

编辑:我正在使用同一域下的两个应用程序(http://app1.domain.com和http://app2.domain.com).

由于我无法使用通配符进行凭证转移请求,因此我检测到Origin并在此域的每个请求中设置Allow-Origin.在我的客户端应用程序中,我有这个代码来发出http请求:

jQuery.extend( {
postJSON: function ( _url, _data, _callback) {
    $.ajax({
        cache: false,
        crossDomain: true,
        url: _url,
        data: _data,
        type: 'POST',
        dataType: 'json',
        xhrFields: {
           withCredentials: true
        },
        headers : {
            "x-requested-with" : "XMLHttpRequest"
        },
        success: _callback,
        error: function() {
            _msg = "<strong>Error: </strong> Error en la petición HTTP (nivel de protocolo).";
            _error( _msg );
        }
    });
}

});

为了处理请求,我有以下方法

@decorators.notAllowed
def get(self):
    pass

@decorators.isNotLogged
@decorators.language
def post(self):
    common._responseJSON( self, CU._doLogin( self ) )

def options(self):
    common._responseJSON( self, CU._doLogin( self ) )

这是OPTIONS请求和响应:

Request URL:http://myapi/method
Request Method:OPTIONS
Status Code:200 OK

Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-ES,es;q=0.8
Access-Control-Request-Headers:origin, x-requested-with, content-type, accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:myapi
Origin:http://myapp
Referer:http://myapp/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11

Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:origin, x-requested-with, content-type, accept
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:http://myapp
Cache-Control:no-cache
content-encoding:gzip
Content-Length:114
Content-Type:application/json
Content-Type:text/html; charset=utf-8
Date:Fri, 16 Nov 2012 11:31:40 GMT
Server:Google Frontend
vary:Accept-Encoding

这是HTTP POST请求:

    Accept:application/json, text/javascript, */*; q=0.01
Content-Type:application/json; charset=UTF-8
Origin:http://myapp
Referer:http://myapp
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
x-requested-with:XMLHttpRequest

但是当浏览器尝试执行POST请求时,它会失败:

XMLHttpRequest cannot load http://myapi/method/. Origin http://myapp is not allowed by Access-Control-Allow-Origin.

任何的想法?我对这个问题感到疯狂……我在OPTIONS http请求中要做什么?也许我没有正确处理它……: – /

提前致谢.

解决方法:

我找到了解决方案!!这很简单;我觉得有点愚蠢…如果你看一下代码,你会在一些方法处理程序之前看到一些装饰器.这就是问题:有时我通过装饰器将内容发送到浏览器.此内容没有Allow-Control- *标头.这就是为什么有时它失败而有时不失败的原因(当装饰者回复时失败)

我已经在所有发送内容的装饰器中配置了标头,它工作正常:-).感谢大家的帮助,真的!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐