[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. Error When calling WebApi
When calling WebApi from my other project I am getting this error. 从我的其他项目调用WebApi时,出现此错误。
XMLHttpRequest cannot load http://localhost:64678/api/Employees/RetriveAllEmployees. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49169' is therefore not allowed access. The response had HTTP status code 500.
I know CORS needs to enables in WebApi project. 我知道CORS需要在WebApi项目中启用。 I have already added bellow code in my WebApi Project. 我已经在WebApi项目中添加了以下代码。
EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
but still I am getting this error when calling WebApi from my other project. 但是从其他项目调用WebApi时仍然出现此错误。
I calling webapi like bellow. 我像下面这样叫webapi。
$("#getEmployees").on("click", function () {
$.ajax({
type: 'GET',
url: 'http://localhost:64678/api/Employees/RetriveAllEmployees',
success: function (data) {
debugger;
},
error: function (error) {
debugger;
}
});
});
Not able to understand what is problem. 无法理解是什么问题。
did you try to add to your webconfig? 您是否尝试添加到您的webconfig?
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
You can try this also. 您也可以尝试一下。
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
I had tried everything I could find on the net including the methods that have been given on this answer. 我尝试了所有可以在网上找到的方法,包括此答案中给出的方法。 After almost trying to solve the problem for whole day I have found the solution that have worked for me like a charm. 在几乎试图解决一整天的问题之后,我发现了对我有用的解决方案,就像是一种魅力。
in the file WebApiConfig in folder App_Start, comment all the lines of code and add the following code: 在文件夹App_Start中的WebApiConfig文件中,注释所有代码行并添加以下代码:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
var enableCorsAttribute = new EnableCorsAttribute("*",
"Origin, Content-Type, Accept",
"GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
//routeTemplate: "api/{controller}/{id}",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Add(new BrowserJsonFormatter());
}
public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
public BrowserJsonFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
this.SerializerSettings.Formatting = Formatting.Indented;
}
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/json");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.