2017-07-19 3 views
0

私はバックボーンで作られたアプリを持っています。コレクションが同期しようとすると、私は次のエラーを取得する:CORSを使用し、資格情報を持たないバックボーンコレクション

XMLHttpRequest cannot load https://someserver/menu.json. The value of 
the 'Access-Control-Allow-Origin' header in the response must not be 
the wildcard '*' when the request's credentials mode is 'include'. 
Origin 'http://www.otherdomain.com' is therefore not allowed access. 
The credentials mode of requests initiated by the XMLHttpRequest is 
controlled by the withCredentials attribute. 

コレクションだけでモデルやURLを指定して、簡単な解析を持っています。

次のように私は、同期メソッドをオーバーライドしてみました:アプリが初期化するとき

(function() { 
    var proxiedSync = Backbone.sync; 
    Backbone.sync = function(method, model, options) { 
    options || (options = {}); 
    if (!options.crossDomain) { 
     options.crossDomain = true; 
    } 
    if (!options.xhrFields) { 
     options.xhrFields = {withCredentials:true}; 
    } 
    return proxiedSync(method, model, options); 
    }; 
})(); 

私はこの機能を実行します。ただし、エラーは同じままです。私は異なる組み合わせ(withCredentials:falsewithCredentialsなどの行を削除してみました)を成功させました。

サービスは「*」を提供するように設定されています。

この問題を解決する方法はありますか?

サービスの再設定やブラウザセキュリティの無効化はオプションではありません。

答えて

0

コードはxhrFieldsが空の場合のみ実行されますが、そうでない可能性があります。

これは一時的な問題を解決します

if (!options.xhrFields) { 
    options.xhrFields = {}; 
} 
options.xhrFields.withCredentials = false; 

のようなものを試してみてください。実際のアプリケーションでは、資格情報が必要です。実際に問題を解決するには、サーバーにドメインのホワイトリストを追加する必要があります。

+0

回答ありがとうございます。残念ながら、サーバーを再構成することはできず、*に設定されています。私はまだwithCredentialsオプションを無効にする方法を取得しません。 –

+0

@ grahamo'donnelどのバージョンのjQueryを使用していますか?これは1.5で追加されました。そして、このコードはいつ実行されますか? –

+0

j jQuery v2.1.4を使用しています。スクリプトsrcタグの直後に、index.htmlにsyncスクリプトを追加しました。 –

関連する問題