Cross-Origin-request Blocked - Missing Token' Access-Control-Allow-Headers' (没有Web.config)

时间:2017-08-15 13:37:14

标签: c# angular typescript asp.net-web-api cors

关于这个问题有很多问题,但这些问题没有答案可以解决我的问题,因为我在谷歌的第3页迷失了,现在是时候问一个问题.. < / p>

问题1: 我正在尝试从localhost向外部api发出请求。

对于外部API,CORS预检返回200,但实际的GET方法永远不会被触发: enter image description here

服务:

@ManyToOne

组件

   /**
     * 
     * @param postalCode
     * @param housenumber
     */
    getAddressFromPostalCode(postalCode: string, housenumber: string) {
        // Initialize to empty object
        this.addressFromPostalCode = '';

        //  Search parameters
        let params: URLSearchParams = new URLSearchParams();
        params.set('postcode', postalCode);
        params.set('number', housenumber);

        //  Apply headers
        let headers = new Headers();
        headers.append('Access-Control-Allow-Headers', 'Content-Type');
        headers.append('Access-Control-Allow-Methods', 'GET');
        headers.append('Access-Control-Allow-Origin', '*');
        headers.append('X-Api-Key', this.apiKey);

        //  Request Options
        let options = new RequestOptions({ headers: headers, search: params });

        //  Get request
        return this.http.get(this.basePostalCodeUrl, options)
            .map(response => response.json());
    }

似乎服务失败,因为它没有在组件中返回结果。

我的Startup.cs

getAddressFromPostalCode() {
        this.isRequesting = true;

        this.postalCodeService.getAddressFromPostalCode(this.contract.postalCode, this.contract.houseNumber)
            .finally(() => this.isRequesting = false)
            .subscribe(
                result => {
                    if (result) {
                            this.deliveryAddress.street = JSON.stringify(result)
                        } else {
                            this.delivery.street = 'No address found';
                        }
                    },
                    error => this.errors = error
            );

    }

问题2: 将数据发布到我的内部ASP WEB API时,我收到相同的// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // ******************** // Setup CORS // ******************** // Add service and create Policy with options services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin() .AllowAnyMethod() //corsBuilder.WithOrigins( //"http://localhost:4200", //"http://localhost:5000", //"https://api.postcodeapi.nu/v2/addresses" .AllowAnyHeader() .AllowCredentials()); }); // Adds the services required for building services.AddMvc(); ... // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // Apply Cors to all requests app.UseCors("AllowAll"); app.UseExceptionHandler(appBuilder => { appBuilder.Run(async context => { context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); context.Response.StatusCode = 500; await context.Response.WriteAsync("Internal Server Error"); }); }); ... app.UseMvc(); 错误,但是 确实将数据发布到服务器(很奇怪)

提前致谢,

编辑: 使用firefox插件时接受来自问题1的请求:https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

但为什么?

0 个答案:

没有答案