4

AppEngineの2つのアプリケーション間でxドメインリクエストを実行しようとしています。一方では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)で両方のアプリケーションを使用しています。

クレデンシャルの転送を伴うリクエストにワイルドカードを使用できないため、オリジンを検出し、このドメインの各リクエストに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

そしてこれはHTTPPOSTリクエストです:

    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.

何か案が?私はこの問題に夢中になっています...OPTIONShttpリクエストで何をしなければなりませんか?多分私はそれを正しい方法で扱っていません...:-/

前もって感謝します。

4

2 に答える 2

2

I found the solution!! It was so simple; i feel a little bit stupid... If you look at the code, you will see some decorators before some method handlers. That's the problem: Sometimes i send content to the browser trough the decorators. And this content has no Allow-Control-* headers. And this is the reason why sometimes it failed and sometimes not (it fails when the decorator replies)

I have configured the headers in all the decorators which send content and it works fine :-). Thanks to all for your help, really!!

于 2012-11-17T23:42:33.790 に答える
1

I just had the same error with Google APIs. Since only 3 or 4 days, all CORS requests to Google APIs fail if they contain the x-requested-with:XMLHttpRequest header, causing that "Origin [...] is not allowed by Access-Control-Allow-Origin" error. It was working until those last days.

You may have the same problem with App Engine CORS requests. Try removing the following lines :

headers : {
        "x-requested-with" : "XMLHttpRequest"
    },
于 2012-11-17T18:31:43.460 に答える