Angularjs 对web api的$http.get调用

Angularjs 对web api的$http.get调用,angularjs,asp.net-mvc,http,cors,http-get,Angularjs,Asp.net Mvc,Http,Cors,Http Get,我有两个web应用程序正在运行。App1是一个angular SPA,App2是一个用c#编写的MVC web api。我正在从Visual Studio 2015执行这两个应用程序,并在IIS Express中运行调试 我的angular代码(App1)正在尝试使用以下(调试)代码调用App2中的api控制器: 我总是点击“alert(err);”行-它从未成功执行,err中没有任何有用的内容来指示问题可能是什么 在Postman(addin for Chrome)中,我可以确认我尝试拨打的A

我有两个web应用程序正在运行。App1是一个angular SPA,App2是一个用c#编写的MVC web api。我正在从Visual Studio 2015执行这两个应用程序,并在IIS Express中运行调试

我的angular代码(App1)正在尝试使用以下(调试)代码调用App2中的api控制器:

我总是点击“alert(err);”行-它从未成功执行,err中没有任何有用的内容来指示问题可能是什么

在Postman(addin for Chrome)中,我可以确认我尝试拨打的App2的电话工作正常。我做错了什么?这可能是CORS的问题吗


提前谢谢

您可能遇到CORS/同源策略问题,在angular js中可以找到相关信息:。
以下是您在服务器站点中处理此问题的方式:


或者您最好打开控制台选项卡中的开发人员工具,为我们提供有关代码中发生的情况的更多信息。

好的,我遇到的问题是web api(MVC 6-ASP.net 5)中,我必须允许来自我的web站点的请求。startup.cs文件添加了以下内容:

        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            services.AddCors();

            StartupInitialize(services);
        }

        // 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)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseIISPlatformHandler();

            app.UseStaticFiles();

            app.UseCors(builder => builder.WithOrigins("http://localhost:12345/"));

            app.UseMvc();

            antiForgery = (IAntiforgery)app.ApplicationServices.GetService(typeof(IAntiforgery));
        }

同一原产地政策?尽管
err
中没有任何有用的内容,但还是可以发布内容。另外,由于您在调试器中运行了两个站点,那么控制器操作是否真的会被命中?方案是
HTTPS
。您确定没有身份验证问题吗?
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            services.AddCors();

            StartupInitialize(services);
        }

        // 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)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseIISPlatformHandler();

            app.UseStaticFiles();

            app.UseCors(builder => builder.WithOrigins("http://localhost:12345/"));

            app.UseMvc();

            antiForgery = (IAntiforgery)app.ApplicationServices.GetService(typeof(IAntiforgery));
        }