我试图在.NET中创建一个简单的POST请求来创建一个带有API的帐户,但是标题显示它会失败并发出警告。 在Postman(api测试工具)中执行相同的请求返回状态200:好的,一切正常,数据保存到数据库。 但我不能在我的网络应用程序中做同样的事情,这是我的代码:
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) => {
});
}
然后我的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);
});
});
}
答案 0 :(得分:2)
您需要在API项目中启用No-Access-Control-Allow-Origin。 在API Project中,startup.cs:
public void Configure(IApplicationBuilder app){
app.UseCors(builder=>builder.AllowOrigin().AllowAnyMethod().AllowAnyHeader());
}