i have web application with multi layer (Business Layer,Data Access Layer ,Service Layer and Ui Layer)
i call service layer (web api) in Ui Layer Razor pages(Using JQuery And Ajax )
as
<button id="btn">ddd</button>
script type="text/javascript">
$(document).ready(function () {
var filmtable = $("#DataTableHtmlId");
$('#btn').click(function() {
$.ajax({
type: 'GET',
dataType: "json",
contentType: 'application/json; charset=utf-8',
url: 'https://localhost:11111/api/film/1',
withCredentials: true,
AccessControlAllowCredentials: true,
success: function(data) {
alert(data);
},
error: function(data) {
alert("err");
}
});
});
} );
</script>
when call controller that fire the view that have last ajax calling
Error This Appear
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:11111/api/film/1. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:11111/api/film/1. (Reason: CORS request did not succeed).
After Search i found many solution as Registering Cors In ConfigureServices as
//Cors policy is added to controllers via [EnableCors("CorsPolicy")]
//or .UseCors("CorsPolicy") globally
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
and Apply CORS In Configure as
//Apply CORS.
app.UseCors("CorsPolicy");
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
But error Also Appear Any help?
Cors
configuration? You need to add it to api project instead of ui project. – Edward