2

I was trying to make a simple POST request to create an account with API in .NET, but it fails with warning as the title says. Doing the same request in Postman(api testing tool) returns status 200: OK, everything works fine this way and data got saved to the database. But I cant do the same in my web application, this is my code:

  register(){

let details = {
  Serviceurl: this.serviceUrl,
  CompanyName: this.companyName,
  CompanyFullName: this.companyFullName,
  LanguageCulture: this.languageCulture,
  IsNewUser: this.isNewUser,
  User: {
    UserName: this.userName,
    FirstName: this.firstName,
    LastName: this.lastName,
    Email: this.email,
    Password: Md5.hashStr(this.password)
  }
}

this.authService.createAccount(details)
.then((result) => {

}, (err) => {

});
}

and then the request itself in my authService:

  createAccount(details){

return new Promise((resolve, reject) => {

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    this.http.post('http://SITEADDRESS/api/IM_Customers/CreateNew', JSON.stringify(details), {headers: headers})
      .subscribe(res => {
        let data = res.json();
        resolve(data);

      }, (err) => {
        reject(err);
      });

});

}

  • 1
    Have you tried to Google it? It's caused by the CORS policy of browsers. Postman doesn't require it. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5 – Ján Halaša Apr 12 '17 at 12:22
  • 1
    Possible duplicate of [No 'Access-Control-Allow-Origin' header in Angular 2 app](http://stackoverflow.com/questions/36002493/no-access-control-allow-origin-header-in-angular-2-app) – n00dl3 Apr 12 '17 at 12:23
  • yeah i tried solving it with google, and i thought i have this enabled and something is wrong with my client side –  Apr 12 '17 at 13:40

1 Answers1

2

You need to enable No-Access-Control-Allow-Origin in API project. In API Project, startup.cs:

public void Configure(IApplicationBuilder app){
    app.UseCors(builder=>builder.AllowOrigin().AllowAnyMethod().AllowAnyHeader());
}
Venkateswaran R
  • 438
  • 2
  • 5
  • 18