0

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) {

    });

}
3
  • 1
    that is IP number not port. Commented Sep 6, 2017 at 10:48
  • An URL format with port is like this: '172.16.209.121:8000' if you not specify the ':8000' by default it asumes port number 80, so, if you want to do an ajax request to a specific IP and port the syntax should be: 'xx.xx.xx.xx:portNumber' Commented Sep 6, 2017 at 10:52
  • I created vertuval Directory,there is no port number for that Commented Sep 6, 2017 at 11:05

1 Answer 1

1

You should use cors.

Install-Package Microsoft.AspNet.WebApi.Cors

App_Start/WebApiConfig - Register method:

config.EnableCors();

FooController:

[EnableCors(origins: "*", headers: "*", methods: "*")]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.