none
Unable to call Web Api from Angular application...

    Question

  • Hi all I am having problem in calling WEB API from angular application. the error I am getting is 



    Failed to load http://localhost:53422/api/TranDetail/SELECTTransForAmendment: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.




    Code calling WEB API from ANGULAR:

    getAllFailedTransactions()
    {
    const headerJson = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Access-Control-Allow-Origin': '*',
    };

    const headersConfig = new HttpHeaders(headerJson);


    return   this.http.get(this.rootUrl + '/api/TranDetail/SELECTTransForAmendment', {
    headers: headersConfig
    });
    }

    WEB API Action:

    [HttpGet]
    [Route("api/TranDetail/SELECTTransForAmendment")]
    public RemitHomeObject<List<RH_TRANDETAIL>> SELECTTransForAmendment()
    {
    RemitHomeObject<List<RH_TRANDETAIL>> objRemitHomeObject = null;

    try
            {
    objRemitHomeObject = new Core().RH_TRANDETAIL_SELECTTransForAmendment();
            }
            catch (Exception ex)
            {
    objRemitHomeObject.APIException = ex;

    throw;
    }

    return objRemitHomeObject;
    }

    Code Written WEB API Config:

    public static class WebApiConfig
    {
    public static void Register(HttpConfiguration config)
    {

    var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors(cors);

    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
    }
    }


    Please help to get this issue resolve.

    Friday, October 12, 2018 7:14 AM