2017-05-25 1 views
1

Polymer 2.0でiron-ajaxを使用する際に問題が発生しています。私のコードはPolymer 1.0に基づいており、私はそれを適応させようとしています。私はこのようなPOSTを通じて自分のフォームを送信します。Iron-ajax 401不正またはCORSの問題

テンプレート:

 <div class="wrapper-btns"> 
      <paper-button raised class="primary" on-tap="postLogin">Log In</paper-button> 
      <paper-button class="link" on-tap="postRegister">Sign Up</paper-button> 
     </div> 

コード:

_setReqBody() { 
     this.$.registerLoginAjax.body = this.formData; 
    } 

    postLogin() { 
     this.$.registerLoginAjax.url = 'http://localhost:3001/sessions/create'; 
     this._setReqBody(); 
     this.$.registerLoginAjax.generateRequest(); 
    } 

鉄 - Ajaxのセットアップ:

<iron-localstorage name="user-storage" value="{{storedUser}}"></iron-localstorage> 
    <app-data key="userData" data="{{storedUser}}"></app-data> 

    <iron-ajax 
     id="registerLoginAjax" 
     method="post" 
     content-type="application/json" 
     handle-as="text" 
     on-response="handleUserResponse" 
     on-error="handleUserError"></iron-ajax> 

そして、私はを次のエラーが表示されます。

POST http://localhost:3001/sessions/create 400 (Bad Request)

そして私は鉄のAjaxにこの行を使用します。

with-credentials="true" 

エラーそれはそうとはCORSの問題である:

XMLHttpRequest cannot load http://localhost:3001/sessions/create . Response to preflight request doesn't pass access control check: 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://127.0.0.1:8081 ' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

私が間違って何をしているのですか?

+0

は、サーバー側では何がありますか?それはポリマー1で正しく働いていますか?*? – Dmitry

答えて

2

の変更ではなく、それが今やっているようレスポンスヘッダAccess-Control-Allow-Origin: *を送り返すよりも、http://127.0.0.1:8081/からの要求に対する応答にレスポンスヘッダAccess-Control-Allow-Origin: http://127.0.0.1:8081/を送信するhttp://localhost:3001/sessions/createバックエンドのサーバー側のコード。

Credentialed requests and wildcards section of the MDN page on CORSは、理由を説明する:

When responding to a credentialed request, the server must specify an origin in the value of the Access-Control-Allow-Origin header, instead of specifying the " * " wildcard.

関連する問題