【问题标题】:Angularjs - unable to call web api service from another project of same solution - VS2015Angularjs - 无法从同一解决方案的另一个项目调用 Web api 服务 - VS2015
【发布时间】:2017-03-04 05:47:34
【问题描述】:

这对你来说可能是一个简单的场景,但对你来说不是,我尝试在网上搜索这个但没有找到任何有用的东西。

今天我遇到了一个奇怪的场景。我编写了一个 Web api 服务,它返回一个 JSON 对象。我在相同的解决方案但在不同的项目下为 AngularJS 应用程序创建了另一个项目。 当我从 Angular 应用程序调用 $http 服务以从 web api 获取数据时,它不起作用。最初我只使用 chrome 和 firefox。 但是当我在 IE 浏览器中打开 Angular 应用程序时,它就起作用了。这是非常奇怪的场景。

同时,我在 web api 项目中编写了一个 angular 代码,即使我在 chrome 或 firefox 中打开了 angular 应用程序,该 angular 代码也能够获取数据。

如果项目不同,任何人都可以指导为什么 chrome 或 firefox 无法获得详细信息。

角度代码

 var app = angular.module("app", [])
            .controller("controller", function ($scope, $http, $log) {
                $http({
                        url: 'http://localhost:37103/api/employee',
                        method: 'GET',
                        headers: { 'Content-Type': 'application/json' }

                    })
                    .then(function success(response) {
                      //  $scope.employees = response.data;
                        $scope.data = response.data;
                    })
                    .then(function error(response) {
                        $scope.error = response;
                    })

            });

HTML:

 <body ng-controller="controller">
Data:{{data}} 
</br>
Error: {{error}}

 </body>

WebApi 方法存在于同一解决方案下的单独项目中

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Web.Http;
 using MyWebAPIService.Models;

 namespace MyWebAPIService.Controllers
 {
public class EmployeeController : ApiController
{
    // GET: api/Employee
    Employee[] employees = {
        (new Employee { Id=1,Name="A",Gender="Male",Salary=77777}),
        (new Employee { Id=2,Name="B",Gender="Male",Salary=2222}),
        (new Employee { Id=3,Name="C",Gender="Female",Salary=55555}),
        (new Employee { Id=4,Name="D",Gender="Male",Salary=78787}),
        (new Employee { Id=5,Name="F",Gender="Female",Salary=8888}),
        (new Employee { Id=6,Name="G",Gender="Female",Salary=4444}),
        (new Employee { Id=7,Name="H",Gender="Male",Salary=3333}),
        (new Employee { Id=8,Name="I",Gender="Male",Salary=77777}),
        (new Employee { Id=9,Name="J",Gender="Female",Salary=45897})
    };

    public IEnumerable<Employee> Get()
    {
        return employees;
    }

    // GET: api/Employee/5
    public Employee Get(int id)
    {
        foreach(Employee e in employees)
        {
            if(e.Id == id)
            {
                return e;
            }
        }
        Employee temp = new Employee();
        temp.Id = -1;
        return temp;

    }
}

}

【问题讨论】:

  • 请贴出相关代码位。不看代码真的很难理解实际问题是什么。
  • @frishi > 查询中插入的代码。

标签: angularjs web-services asp.net-web-api visual-studio-2015


【解决方案1】:

看起来问题出在异步调用上,在以下响应的帮助下,我能够解决我的问题。

 http://stackoverflow.com/a/40191484/1997983

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2013-09-06
    • 2020-05-08
    • 2013-10-16
    • 2016-10-29
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多