CORS在本地pc上从angular应用程序发布到asp.net核心API
我有一个本地运行的ASP.NET核心API服务和一个本地Angular应用程序。我的大多数调用对API来说都很好,但是一个调用总是给我CORS错误。我怀疑是因为这是一次手术后?根据CORS,POST要求有什么不同吗 Anuglar ts:CORS在本地pc上从angular应用程序发布到asp.net核心API,angular,asp.net-core,asp.net-core-2.0,Angular,Asp.net Core,Asp.net Core 2.0,我有一个本地运行的ASP.NET核心API服务和一个本地Angular应用程序。我的大多数调用对API来说都很好,但是一个调用总是给我CORS错误。我怀疑是因为这是一次手术后?根据CORS,POST要求有什么不同吗 Anuglar ts: loadGroup(groupName:string){ this.apiService.getInfluencersWithFilter(this.query,groupName,null,null,this.ageRanges).subscribe
loadGroup(groupName:string){
this.apiService.getInfluencersWithFilter(this.query,groupName,null,null,this.ageRanges).subscribe((data) => {
this.influencerSearchResult = data.results;
// this.searchGroupsFacet = data.facetGroupList;
this.searchSubGroupsFacet = data.facetSubGroupList;
this.showSubGroups = true;
});
}
角度服务:
getInfluencersWithFilter(q:string, group:string, subGroups:string[], socialAccounts:string[],ageRanges:AgeRange[]):Observable<InfluencerSearchContainer>
{
if(q==null)
{
q = "";
}
var url = `${environment.apiDomain}/api/InfluencersSearch/`;
return this.httpClient.post<InfluencerSearchContainer>(url,{q:"",group:group,subGroups:subGroups, socialAccounts:socialAccounts, ageRanges:ageRanges}).pipe(
map(x => new InfluencerSearchContainer(x)));
}
我有什么遗漏吗?我想这里的一切都是残废的?在我写这篇文章的时候,我怀疑它与这篇文章有关,因为get操作是有效的
在《邮递员》中:
我还在控制器上添加了以下内容:
[EnableCors(“AllowSpecificOrigin”)]有时
1) 如果请求负载中有错误(或api中有错误),chrome开发者工具控制台将显示CORS
请console.log请求并与postman/swagger一起单独测试api
如果1)没有解决问题
2) 有时IIS在默认情况下可以阻止PUT和POST操作。请看
如果1)和2)没有解决问题
这也可能是一个问题
3) 我们可能必须将[EnableCors(“AllowSpecificCorigin”)]添加到控制器中
可能会抛出一些其他错误,API返回的是CORS错误,而实际上它不是CORS错误。您应该在进行API调用时调试API,以查看是否有问题。谢谢,我已经尝试了postman并启用了属性:(我将查看项目符号2。如果postman有效,IIS允许POST。第2点)有效。我怀疑UI服务。关于2)有意义。所以你认为这是有角度的部分?我要看看《小提琴手》,你说得对。Fiddler显示它与CORS无关,我收到以下错误:FileNotFoundException:无法加载文件或程序集“Newtonsoft.Json,Version=12.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6eed”。系统找不到指定的文件。我更新了Newton nuget软件包,并遵循它进行了操作。我不明白邮递员是怎么工作的,但那个瘦削的人没有,那真是太奇怪了。但它现在起作用了,这才是最重要的。谢谢你们,非常感谢。
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<DocumentClient>((s) =>
{
string EndpointUrl = Configuration["CosmosDB:EndpointUrl"];
string PrimaryKey = Configuration["CosmosDB:PrimaryKey"];
return new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
});
var connStr = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<DB>(options => options.UseSqlServer(connStr));
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors("AllowSpecificOrigin");
app.UseHttpsRedirection();
app.UseMvc();
}
[HttpPost]
public InfluencerSearchResultWithFacets Post([FromBody] InfluencerQuery q)
{
return GetSearchResult(q.q, q.group,q.subGroups, q.socialAccounts, q.ageRanges);
}