简体   繁体   English

Angular 4 / Asp.net Web API-不存在“ Access-Control-Allow-Origin”标头

[英]Angular 4 / Asp.net Web API - No 'Access-Control-Allow-Origin' header is present

I'm trying to call the API from angular and getting this errors. 我试图从角度调用API,并得到此错误。 I donot want to set proxy in angular instead allow CORS in Angular 4 and Web API. 我不想在angular中设置代理,而是在Angular 4和Web API中允许CORS。

Following is my code: 以下是我的代码:

let reHeader: RequestOptions = new RequestOptions();
        reHeader.headers = new Headers();
        reHeader.headers.append("content-type", "application/json");
        reHeader.headers.append("x-api-key", store.APIKey);
        reHeader.headers.append('Access-Control-Allow-Origin','*');
        reHeader.headers.append('Access-Control-Allow-Methods', "GET, POST, PATCH, PUT, DELETE, OPTIONS")
        reHeader.headers.append('Access-Control-Allow-Headers', "Origin, Content-Type, X-Auth-Token")
        switch (rtype) {
            case "get":
                let query: string = "";
                action.ActionParams.forEach(m => {
                    query += "&" + m.Key + "=" + m.Value;
                });
                url = url + query;
                return this.http.get(url, reHeader);
            default:
                return Observable.of(null);
        }

and from WEB API in startup.cs 并从startup.cs中的WEB API

    public void Configuration(IAppBuilder app)
            {
                app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// ASPNETCORE_ENVIRONMENT should be set to 'Development'
            if (env != null && env.IsDevelopment())
            {
                app.UseErrorPage(ErrorPageOptions.ShowAll);
            }

            var config = new HttpConfiguration();
            ConfigureWebApi(config);
            // Swagger
            SwaggerConfig.Register(config);
            // Register routes
            WebApiConfig.Register(config);
             }

I have checked other posts but still didnt get solution. 我检查了其他帖子,但仍未获得解决方案。 - Angular 2 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header -Angular 2对预检请求的响应未通过访问控制检查:没有“ Access-Control-Allow-Origin”标头

Add below code in Global.asax and try 在Global.asax中添加以下代码,然后尝试

protected void Application_BeginRequest(Object sender, EventArgs e)
        {

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Pragma, Cache-Control, Authorization ");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不存在“Access-Control-Allow-Origin”标头 - 面向 .net 框架 4.5.1 的 asp.net 核心 Web api - No 'Access-Control-Allow-Origin' header is present - asp.net core web api targeting .net framework 4.5.1 ASP.NET Web窗体:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ASP.NET Web Forms: No 'Access-Control-Allow-Origin' header is present on the requested resource 在Asp.net Core 2.1的Angular 6中不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present Angular 6 with Asp.net Core 2.1 Access-Control-Allow-Origin 在标头请求 ASP.NET Web API 上出现两次 - Access-Control-Allow-Origin appearing twice on header request ASP.NET Web API Web API 2 CORS不存在“ Access-Control-Allow-Origin”标头 - web api 2 CORS No 'Access-Control-Allow-Origin' header is present Angular.js-CORS-ASP.NET-Rest API-在Access-Control-Allow-Origin标头中找不到原始 - Angular.js - CORS - ASP.NET - Rest API - Origin not found in Access-Control-Allow-Origin header ASP.Net核心WebAPI - 请求的资源上没有“Access-Control-Allow-Origin”标头 - ASP.Net Core WebAPI - No 'Access-Control-Allow-Origin' header is present on the requested resource ASP.NET Core Web Api发送Access-Control-Allow-Origin:null CORS头和chrome是错误的,如何修复? - ASP.NET Core Web Api sending Access-Control-Allow-Origin: null CORS header and chrome is erroring, how to fix? 将文件上传到 ASP.NET Web API 时出现 Access-Control-Allow-Origin HTTP 500 错误 - Access-Control-Allow-Origin HTTP 500 error when Upload file to ASP.NET Web API In.Net Core 2.1 Web API 用于 PUT 和 DELETE,仅为什么请求的资源上存在“No 'Access-Control-Allow-Origin' header” - In .Net Core 2.1 Web API for PUT and DELETE only why “No 'Access-Control-Allow-Origin' header is present on the requested resource”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM