I got XmlHttpRequest object, and i'am trying to send long html data string to Asp Net Core, to make out of it content PDF file. But still getting CORS policies. Even-though i have "Access-Control-Allow-Origin" in my header, it still an issue for me. Already tried everything with CORS. Installed cors for Asp net Core, nothing changed. Everything works fine if i use my HTML document from local.
Full error:
Access to XMLHttpRequest at 'https://.../getInfoWindowPdf?' from origin 'https://...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
function infoWindowPdf()
{
// Create request
let http = new XMLHttpRequest(); //XMLHttpRequest XDomainRequest
let url = backend + "getInfoWindowPdf?";
// Add in htmlContent header
let htmlContent = "<style>" + style +"</style>";
// Get needed content
let infoWindow = document.querySelector("#section-library-modal");
//Waits for backend to create file after all open it and remove created temporary files
http.onreadystatechange = function()
{
if(http.readyState == 4)
{
window.open(backend + 'InfoPdf?filePath=C:\\Projects\\Web\\WebSections\\wkhtmltopdf\\bin\\pdf-export\\' + sectionName + ".pdf", '_blank');
setTimeout(() => {getDBData('removepdf?filePath=C:\\Projects\\Web\\WebSections\\wkhtmltopdf\\bin\\pdf-export\\&filename=' + sectionName);}, 100);
}
};
http.open("POST", url, true);
http.setRequestHeader('Access-Control-Allow-Origin', '*');
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //application/x-www-form-urlencoded
http.send(params);
}
My Asp Net Core Startup Config for CORS.
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://localhost:5000",
"http://localhost:5000/websections/getInfoWindowPdf?"
).AllowAnyHeader().AllowAnyMethod();
});
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseCors(MyAllowSpecificOrigins);
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Edit: Do not even try to work with Xmlhttprequest, if you want send some data to your backend project. Just use backend straight.