Reputation:
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);
});
});
}
Upvotes: 2
Views: 5905
Reputation: 478
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());
}
Upvotes: 2