0

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
6
  • You need to configure your server properly stackoverflow.com/questions/10143093/… Commented Aug 4, 2016 at 11:45
  • with Angular 1.x all work well. Pls read my questions again. Commented Aug 4, 2016 at 11:46
  • How is contentHeaders defined? Commented Aug 4, 2016 at 11:51
  • Angular doesn't send an OPTIONS request. This request is created by the browser. Commented Aug 4, 2016 at 11:51
  • The headers need to be added by the server to the response to the OPTIONS request. Adding them in Angular is redundant. Commented Aug 4, 2016 at 11:59

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.