简体   繁体   English

所请求的资源上没有“ Access-Control-Allow-Origin”标头。 调用WebApi时出错

[英]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.

相关问题 调用WebAPI时,请求的资源上不存在Access-Control-Allow-Origin标头 - No Access-Control-Allow-Origin header is present on the requested resource when calling WebAPI 请求的资源上不存在“Access-Control-Allow-Origin”header。 当启用 CORS 时 - No 'Access-Control-Allow-Origin' header is present on the requested resource. when CORS is enabled Angular2:http.get返回“No&#39;Access-Control-Allow-Origin&#39;标头出现在请求的资源上。” - Angular2: http.get returns “No 'Access-Control-Allow-Origin' header is present on the requested resource.” 调用我的 API 时在我的反应应用程序上出现此 CORS 错误 - 请求的资源上不存在“Access-Control-Allow-Origin” header - Getting this CORS error on my react app when calling my API - No 'Access-Control-Allow-Origin' header is present on the requested resource 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http://www.sitename.com” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.sitename.com,' is therefore not allowed access ASP.Net核心WebAPI - 请求的资源上没有“Access-Control-Allow-Origin”标头 - ASP.Net Core WebAPI - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 问题:请求的资源上不存在“Access-Control-Allow-Origin”header - Issue: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource CORS - 没有'Access-Control-Allow-Origin' header 存在于请求的资源上 - CORS - No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM