Reputation: 440
I am new to WebApi and deployed WebAPI in IIS,That service When I call web Api by using IP (http://172.16.209.121/Analytics/api/Purchase/GetProcSmartAnylisys…/Quantity/2ed4b08f-1566-4538-b46a-6fb3a23bf50c/01-01-2017/12-31-2017/daily), I am getting error 'Access-Control-Allow-Origin',When I using localhost(http://localhost/Analytics/api/Purchase/GetProcSmartAnylisys…/Quantity/2ed4b08f-1566-4538-b46a-6fb3a23bf50c/01-01-2017/12-31-2017/daily) it working fine......, How to call by using port number.
This is my 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));
}
}
this is my Angularjs Code
$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) {
});
}
Upvotes: 0
Views: 660
Reputation: 963
You should use cors.
Install-Package Microsoft.AspNet.WebApi.Cors
App_Start/WebApiConfig - Register method:
config.EnableCors();
FooController:
[EnableCors(origins: "*", headers: "*", methods: "*")]
Upvotes: 1