![](/img/trans.png)
[英]No 'Access-Control-Allow-Origin' header is present. But the header is present
[英]No 'Access-Control-Allow-Origin' header is present. XmlHttpRequest
我有XmlHttpRequest對象,而我正試圖將長的html數據字符串發送到Asp Net Core,以使它成為內容PDF文件。 但仍然獲得CORS政策。 盡管我的標題中有“ Access-Control-Allow-Origin”,但對我來說仍然是一個問題。 已經嘗試使用CORS進行所有操作。 為asp net Core安裝了cors,未做任何更改。 如果我使用本地的HTML文檔,則一切正常。
完整錯誤:
訪問位於' https://.../getInfoWindowPdf ?'的XMLHttpRequest 來自源“ https:// ...”的信息已被CORS策略阻止:對預檢請求的響應未通過訪問控制檢查:請求的資源上不存在“ Access-Control-Allow-Origin”標頭。
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);
}
我的CORS的Asp Net Core啟動配置。
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?}");
});
}
編輯:如果要向后端項目發送一些數據,甚至不要嘗試使用Xmlhttprequest。 只需直接使用后端即可。
Install-Package Microsoft.AspNetCore.Cors
在ConfigureServices中添加CORS服務
public void ConfigureServices(IServiceCollection services) { services.AddCors(); }
在Startup.cs文件的配置方法中
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseCors( options => options.WithOrigins("http://example.com").AllowAnyMethod() ); app.UseMvc(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.