我在这方面机智。我已经研究过类似问题的其他答案,因此没有任何运气。
我相当确定我已经正确启用了CORS,以允许来自所有来源的传入请求(在这种情况下为POST请求),但是我看到以下错误:
无法加载http:// localhost:5000 / expenses:所请求的资源上没有“ Access-Control-Allow-Origin”标头。因此,不允许访问源' http:// localhost:4200 '。响应的HTTP状态码为500。
这是我在webAPI项目中启用CORS的方式:
Startup.cs中的相关方法
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
services.AddDbContext<ExpensesDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IBaseDa<Accounts>, AccountsDataAccess>();
services.AddTransient<IExpensesDa, ExpensesDa>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
env.EnvironmentName = "Development";
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => …Run Code Online (Sandbox Code Playgroud) 目标是遍历一组 ID,为每个 ID 进行一次 HTTP 调用。对于每个 ID,我使用了一个带有 get() 方法的服务,该方法返回一个 Observable。每次调用 get() 方法时,我都会订阅返回的 Observable 并尝试将结果推送到一个数组中,该数组最终将传递给新操作的不同方法。
相关服务方式:
public get(departmentId: number): Observable<IDepartmentModel> {
return super.get<IDepartmentModel>(departmentId);
}
Run Code Online (Sandbox Code Playgroud)
注意:超类正在利用 Angular Http,它经过充分测试并确认可以正常工作。逻辑的问题不在这里......
相关组件方法: 注意在 forEach 中多次调用的 DepartmentService.get() 调用。
setInitialDepartmentsAssignedGridData(): void {
this.settingsForDropdownSelectedCompanyId = this.userForm.get('defaultCompany').get('defaultCompanyId').value;
let departments: IDepartmentModel[] = [];
this.userService.user.getValue() //confirmed: valid user is being pulled back from the userService (logic is fine here..)
.userCompanies.find(comp => comp.companyId === this.settingsForDropdownSelectedCompanyId) // getting a valid match here (logic is fine here..)
.departmentIds.forEach(deptId => this.departmentService.get(deptId).first().subscribe(dept => { // getting …Run Code Online (Sandbox Code Playgroud) 前言:我要描述的所有内容都可以在本地工作,但当我的代码部署在 Windows 环境中的 Azure Web 应用服务中时,这些内容就不起作用了。另外,我已经阅读了这个问题的答案,我不认为它适用于我的具体情况;我没有使用任何自定义域。
有关更多背景信息:我没有将我的应用程序容器化;我正在使用 Azure DevOps 构建和部署代码。
我有一个相对简单的 Angular 7 (CLI) 应用程序,带有一些简单的路由。我有许多路由模块,它们被导入到各自的功能模块中,然后功能模块被导入到主/核心应用程序模块中:app.module.ts。
在大多数情况下,/aboutUs/ourMission我会将用户导航到诸如或 之类的路线上的组件/support/donations。这些路由在本地和远程都运行良好。
远程效果不佳的是或之类的路线。/admin/register
应用程序模块.ts:
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {HeaderComponent} from './header/header.component';
import {VolunteerComponent} from './volunteer/volunteer.component';
import {TagComponent} from './tag/tag.component';
import {SummerCampComponent} from './summer-camp/summer-camp.component';
import {RegisterComponent} from './register/register.component';
import {FooterComponent} from './footer/footer.component';
import …Run Code Online (Sandbox Code Playgroud) angular ×2
.net ×1
ajax ×1
angular-http ×1
asp.net ×1
asp.net-core ×1
azure ×1
c# ×1
observable ×1
rxjs ×1