2016-11-09 7 views
1

タイトルはように私はサブドメイン上のREST APIとsymfony3アプリケーションを持って、言うように:Symfony3/NelmioのCORS/APIをフェッチ - 400不正な要求

GET http://ajax.localhost.dev:10190/menus 

は、JSON形式のメニューのリストを返します。

export default class AppRemote extends Remote { 
    get host() { return `ajax.${super.host}`; } 
    open(xhr, data) { 
    if ("withCredentials" in xhr) { 
     xhr.open(data.method, this.url(data.url), true); 
     xhr.withCredentials = true; } 

    else if (typeof XDomainRequest != "undefined") { 
     xhr = new XDomainRequest(); 
     xhr.open(data.method, this.url(data.url)); } 

    else { this.app.error('cannot init CORS Request'); } 

    return xhr; 
    } 
}; 

罰金働いていた:

は、ここで私はこのコードを使用しているnelmioのCORSバンドル(/app/config/nelmio/cors.yml

nelmio_cors: 
    paths: 
    '^/': 
     origin_regex: true 
     allow_origin: ['^https?://%domain%:[0-9]+'] 
     allow_headers: ['X-Custom-Auth'] 
     allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] 
     max_age: 3600 
     hosts: ['^ajax\.'] 
     allow_credentials: true 

の構成です。今、私はそれが新しいフェッチAPIに移植しようとしていると私はこのコードを使用しています:

const init = (url, method = 'GET') => { 
    return new Request(url, { 
    headers: new Headers({ 
     'Access-Control-Allow-Credentials': true, 
     //'Access-Control-Allow-Origin': '*', 
     'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS' 
    }), 
    mode: 'cors', 
    credentials: true, 
    method }); 
} 

const get = (url) => { 
    return fetch(init(url)); 
} 

それはRequest Method: OPTIONSで、400 Bad Requestを返します。ブラウザにURLを入力するだけで動作します。

私はいくつかの認証の問題があると思うが、それを解決する方法を見つけることができません。どうやってやるの?

答えて

0

いくつかの調査の後、私はついに間違いを見つけました。 MDN

return new Request(url, { 
    //headers: new Headers({ 
    //'Access-Control-Allow-Credentials': true, 
    //'Access-Control-Allow-Origin': '*', 
    //'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS' 
    //}), //<-- working without headers at all 
    mode: 'cors', 
    credentials: 'include', //<-- that is the right setting 
    method }); 

ドキュメント:スポットに私を指してエラーを投げたのFirefox、おかげで

関連する問題