Click here to Skip to main content
15,888,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When i’m uploading my project on hosting site and data is coming from there only,it worked fine on localhost but when i uploaded my webapi on hosting server it’s not working data is not coming and i’m not able to logged into my application.

Error is below:
1.) Error:
Access to XMLHttpRequest at ‘http://stylen.in/token’ from origin ‘http://localhost:8100’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
2.) Error:
POST http://stylen.in/token net::ERR_FAILED


What I have tried:

I tried different ways, in postman data is coming perfectly and i also able to log in.
Posted
Updated 1-Mar-20 19:45pm
Comments
Viswanatha Swamy 2-Mar-20 1:42am    
Are you accessing the hosted Web API using your application from "http://localhost:8100"? Please advise. In this case you need to add the URL to the CORS section. Please refer to the URL: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api
Member 14704249 2-Mar-20 2:06am    
I go through your link i already did those steps in my Webapi and also in ionic project.(those errors are not coming on my localhost but when i uploaded Webapi than only errors are coming.)
Viswanatha Swamy 2-Mar-20 3:08am    
In your local the error will not come as both the UI and Web API will have http(s)://localhost. Now that you have hosted Web API and trying to access it from your local the domains are different. In this case, you have to whitelist the domains from which you want the Web API to be accessed. When you try through the postman it will go through without any issues. Please refer this URL https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin

1 solution

Are you accessing the hosted Web API using your application from "http://localhost:8100"? Please advise. In this case you need to add the URL to the CORS section. Please refer to the URL: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Sample Code for ASP.net Core just for reference
C#
public void ConfigureServices( IServiceCollection services) 
{ 
	// Add framework services. 
	services.AddMvc(); 

	services.AddCors( options = > {
	 options.AddPolicy("DemoCorsPolicy", c = > c.WithOrigins("http://localhost:8100")); 
	}); 
} 

public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
	app.UseMvc(); 
	app.UseCors("DemoCorsPolicy"); 
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900