2016-08-04 21 views
0

Ich habe einen einfachen Code für senden Post-Anfrage an meinen lokalen Server. So:Angular2 Probleme mit dem Senden von Post-Anfrage

login(event, username, password) { 
    event.preventDefault(); 
    let body = "grant_type=password&username=" + username + "&password=" + password; 
    this.http 
     .post(`${appSettings.API_ENDPOINT_DEV}Login`, body, {headers: contentHeaders}) 
     .subscribe(
     response => { 
      this.accessToken = response.json().access_token; 
      console.log(this.accessToken); 
      localStorage.setItem('access_token', this.accessToken); 
     }, 
     error => { 
      alert(error.text()); 
      console.log(error.text()); 
     } 
    ); 
    } 

Aber ich nächste Ausnahme in der Google Chrome-Entwickler-Konsole fangen.

XMLHttpRequest kann http://localhost:1216/api/Login nicht geladen werden. Antwort für Preflight hat ungültige HTTP-Statuscode 400

mir helfen, dieses Problem zu beheben, bitte, da mit kantigem 1.x alle gut funktionieren. Log von Google Chrome Konsole:

Request URL:http://localhost:1216/api/Login 
    Request Method:OPTIONS 
    Status Code:400 Bad Request 
    Remote Address:[::1]:1216 
    Access-Control-Allow-Origin:* 
    Cache-Control:no-cache 
    Content-Length:34 
    Content-Type:application/json;charset=UTF-8 
    Date:Thu, 04 Aug 2016 11:43:21 GMT 
    Expires:-1 
    Pragma:no-cache 
    Server:Microsoft-IIS/10.0 
    X-Powered-By:ASP.NET 
    X-SourceFiles:=?UTF-8?B?RDpcU291cmNlc1xHcmFwcHNcVUtfQnJhbmRQcm90ZWN0aW9uXFdlYkFQSVxhcGlcTG9naW4=?= 

UPD:

export const contentHeaders = new Headers(); 
contentHeaders.append('Content-Type', 'application/x-www-form-urlencoded'); 
contentHeaders.append('Access-Control-Allow-Origin', '*; *'); 
contentHeaders.append('Access-Control-Allow-Methods', 'POST'); 

UPD2 Log von erweiterte REST-Client

Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: application/json;charset=UTF-8 
Expires: -1 
Server: Microsoft-IIS/10.0 
Access-Control-Allow-Origin: *; * 
X-Sourcefiles: =?UTF-8?B?RDpcU291cmNlc1xHcmFwcHNcVUtfQnJhbmRQcm90ZWN0aW9uXFdlYkFQSVxhcGlcTG9naW4=?= 
X-Powered-By: ASP.NET 
Access-Control-Allow-Headers: Content-Type 
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS 
Date: Thu, 04 Aug 2016 11:59:19 GMT 
Content-Length: 316 
+0

Sie müssen Ihren Server richtig konfigurieren http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control -allow-Herkunft –

+0

mit Angular 1.x funktionieren alle gut. Bitte lies meine Fragen nochmal. –

+0

Wie ist 'contentHeaders' definiert? – kiswa

Antwort

0

ich Probleme gefunden hatte ich einige config die Web.config hinzugefügt:

<httpProtocol> 
      <customHeaders> 
       <add name="Access-Control-Allow-Origin" value="*" /> 
       <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
       <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> 
      </customHeaders> 
     </httpProtocol> 

Und entfernen app.UseCors (CorsOptions.AllowAll); von startup.cs Klasse. Vielen Dank, alle Leute für verbringen Zeit für mein Problem;)