Reputation: 776
I am using Visual Studio code with liveserver for html/javascript, and using VS Studio 2019 for Dot net core Web API I have create site in IIS on Windows 10 locally for the web api.
The login page has following javascript function, that is invoked on login button click event
function fnlogin() {
window.location.href = "http://localhost:5500/productmenu.html"
}
<header>
<ul class = "nav">
<li class="navlink"> <a href="productInfo.html> target="content"> Product Information <a> <li?
<li class="navlink"> <a href="orderHistory.html> target="content"> Order History <a> <li?
</ul>
</header>
Once user selects the menu option "Product Information", another html page is displayed This is coded in productinfo.html
Once the user enters product code and clicks on search , a dot net core web api is invoked and data is displayed in the productinfo.html using fetch api
The api is called as follows in JS
fetch('http://localhost:8082/api/product/' + productId)
.then(response => response(json))
.then((response => {
return response
})
.then ( (response) => {
console.log(response);
})
.catch(err => console.log(err));
method : configureServices
services.AddDefaultPolicy(
options.AddDefaultPolicy (
builder =>
{
builder.WithOrigins("http://127.0.0.1:5500)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
})
)};
The configure method has following
app.UseRouting();
app.UseCors();
app.UseAuthorization():
The web api is published in IIS on site with port 8082
The issue is as follows
I checked the Network tab in Chrome dev tools and see Request headers
Accept: */*
Host: localhost:8082
Origin: http://localhost:5500
Sec-Fetch-Dest: Empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site : same-site
Upvotes: 1
Views: 1407
Reputation: 43959
You have a bug in Cors syntax. This is a syntax for default policy:
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("http://127.0.0.1:5500")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
But if it is stil not working, you can try to replace default Cors policy with named policy
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("AllowOrigins", builder =>
{
builder.WithOrigins("http://localhost:5500")
.AllowAnyMethod()
.AllowAnyHeader();
}));
.....
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
.....
app.UseRouting();
app.UseCors("AllowOrigins");
app.UseAuthorization();
....
}
Upvotes: 1