2017-05-25 6 views
1

Ich habe Probleme mit Eisen-Ajax auf Polymer 2.0. Mein Code basiert auf einem Polymer 1.0 und ich versuche es anzupassen. Ich sende meine Form durch einen POST wie folgt aus:Iron-Ajax 401 nicht autorisierte oder CORS Problem

Vorlage:

 <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> 

Code:

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

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

Eisen-Ajax-Setup:

<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> 

Und wenn ich tun Ich erhalte den folgenden Fehler:

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

Und wenn ich diese Zeile auf dem Eisen-Ajax:

with-credentials="true" 

Der Fehler, da es ein CORS Problem scheint, ist:

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.

Was mache ich falsch?

+0

Was Sie auf Server-Seite haben? Funktioniert es richtig mit Polymer 1. *? – Dmitry

Antwort

2

den Code serverseitige ändern Sie für den http://localhost:3001/sessions/create Backend jetzt die Antwort-Header Access-Control-Allow-Origin: http://127.0.0.1:8081/ in Antworten auf Anfragen von http://127.0.0.1:8081/, anstatt Rücksendung der Antwort-Header Access-Control-Allow-Origin: * wie es tut zu senden.

Der Credentialed requests and wildcards section of the MDN page on CORS erklärt, warum:

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.

Verwandte Themen