如何使用端口号调用WebAPI

时间:2017-09-06 10:45:29

标签: angularjs asp.net-web-api

我是WebApi的新手并在IIS中部署了WebAPI,该服务当我使用IP(http://172.16.209.121/Analytics/api/Purchase/GetProcSmartAnylisys ... / Quantity / 2ed4b08f-1566-4538-b46a-6fb3a23bf50c / 01-01-2017 /来调用web Api时12-31-2017 /每日),当我使用localhost(http://localhost/Analytics/api/Purchase/GetProcSmartAnylisys ... / Quantity / 2ed4b08f-1566-4538-b46a-6fb3a23bf50c / 01-01时,我收到错误'Access-Control-Allow-Origin' -2017 / 12-31-2017 /每日)它工作正常......,如何使用端口号呼叫。

这是我的WebApi

[RoutePrefix("api/Purchase")]

public class PurchaseController : ApiController

{

    [HttpGet]

    [Route("GetProcSmartAnylisysFinal/{treeType}/{basedOn}/{valueOrQty}/{organization}/{fromDate}/{toDate}/{range}")]
    public IHttpActionResult GetProcSmartAnylisysFinal(string treeType, string basedOn, string valueOrQty, string organization, string fromDate, string toDate, string range)

    {

        ProcSmartAnylisysFinal procSmartAnylisysFinal = new ProcSmartAnylisysFinal();

        return Ok(procSmartAnylisysFinal.getProcSmartAnylisysFinal(treeType, basedOn, valueOrQty, organization, fromDate, toDate, range));

    }

}

这是我的Angularjs代码

$scope.submit = function () {

    $http({

        method: "GET",

        url: 'http://172.16.209.121/mbas50-GCGAnalytics/api/Purchase/GetProcSmartAnylisys…/Quantity/2ed4b08f-1566-4538-b46a-6fb3a23bf50c/01-01-2017/12-31-2017/daily',

        datatype: 'json'

    }).then(function (data) {

        if (data.data.length > 0) {
            $scope.result = data.data;                
        } else {
            alert("there is no data found");
        }

    }, function (data) {

    });

}

1 个答案:

答案 0 :(得分:1)

你应该使用科尔。

Install-Package Microsoft.AspNet.WebApi.Cors

App_Start / WebApiConfig - 注册方法:

config.EnableCors();

FooController的:

[EnableCors(origins: "*", headers: "*", methods: "*")]