Taras Kovalenko
Taras Kovalenko

Reputation: 2393

Angular2 problems with sending post request

I have a simple code for send post request to my local server. Like this:

  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());
        }
      );
  }

But I catch next exception in the google chrome dev console.

XMLHttpRequest cannot load http://localhost:1216/api/Login. Response for preflight has invalid HTTP status code 400

Help me please fix this problem because with Angular 1.x all work well. Log from google chrome console:

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

Upvotes: 0

Views: 1909

Answers (2)

Taras Kovalenko
Taras Kovalenko

Reputation: 2393

I found problems I had added some config to the Web.config:

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

And remove app.UseCors(CorsOptions.AllowAll); from startup.cs class. Thanks a lot, all peoples for spend time for my problem ;)

Upvotes: 0

Andrei Zhytkevich
Andrei Zhytkevich

Reputation: 8099

You maybe missing another header:

"Access-Control-Allow-Methods", "POST"

Check CORS enabled but response for preflight has invalid HTTP status code 404 when POSTing JSON for details

Update: Maybe more then just this one. Check this answer https://stackoverflow.com/a/37654548/1267942

Upvotes: 1

Related Questions