简体   繁体   English

Angular $ http.get调用Web API

[英]Angular $http.get call to web api

I have two web applications running. 我有两个正在运行的Web应用程序。 App1 is an angular SPA and App2 is an MVC web api written in c#. App1是一个有角度的SPA,而App2是一个用c#编写的MVC Web api。 I am executing both applications from Visual Studio 2015, running debug in IIS Express. 我正在执行Visual Studio 2015中的两个应用程序,并在IIS Express中运行调试。

My angular code (App1) is trying to call an api controller in App2 using the following (debug) code: 我的角度代码(App1)尝试使用以下(调试)代码调用App2中的api控制器:

$http.get('https://localhost:12345/api/values').then(function (response) {
            alert(response.data);
        }, function (err) { 
            alert(err);
        }).catch(function (e) {
            console.log("error", e);
            throw e;
        }) .finally(function () {
            console.log("This finally block");
        });

I always hit the "alert(err);" 我总是打“警报(err)”; line - it never successfully executes and err has nothing useful in it to indicate what the problem could be. 行-它永远不会成功执行,并且err并没有任何用处来指示问题可能是什么。

In Postman (addin for Chrome), I can confirm the call I'm trying to make to App2 works fine. 在Postman(Chrome的插件)中,我可以确认我尝试对App2进行的呼叫正常运行。 What am I doing wrong? 我究竟做错了什么? Could this be an issue with CORS? 这可能是CORS的问题吗?

Thanks in advance! 提前致谢!

You either experiencing CORS/SAME ORIGIN POLICY issue that some information about it in angular js can be found here: How to enable CORS in AngularJs . 您遇到CORS / SAME原始政策问题,可以在以下位置找到有关angular js的一些信息: 如何在AngularJs中启用CORS
this is how you handle it in server site in your case: http://docs.asp.net/projects/mvc/en/latest/security/cors-policy.html#cors-policy 在您的情况下,这就是在服务器站点中处理它的方式: http : //docs.asp.net/projects/mvc/zh/latest/security/cors-policy.html#cors-policy

or you better open the developer tools in console tab and bring us some more information about what happened in your code. 或者您最好在控制台选项卡中打开开发人员工具,并向我们提供有关代码中发生的情况的更多信息。

Ok the problem I had was in the web api (MVC 6 - ASP.net 5) in that I had to allow the requests from my angular web site. 好的,我遇到的问题是在Web api(MVC 6-ASP.net 5)中,因为我必须允许来自我的有角度网站的请求。 The startup.cs file had the following added: 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));
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM