Server uses Cors but UI still gets allow-origin error
Hello i am trying to issue a http
get
request to a .NET Core Console App
from my Angular 2
frontend and i get the following error:
Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
For me it is curious since i have enabled CORS
on the server side as you can see below in the Startup
class.
Startup
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();
public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseMvc();
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
I make the request from the UI
like this:
@Injectable()
export class UserService
private static baseUrl:string="http://127.0.0.1:9300/api";
constructor(private http:HttpClient)
getClientsAsync():Promise<User>
let route=UserService.baseUrl+"/getusers";
var data=(this.http.get(route) //should i have some headers here?
.map(resp=>resp)
.catch(err=>
Observable.throwError(err)
) as Observable<User>).toPromise<User>();
return data;
P.S I have tried with Postman
and the request works ,however here in the angular 2
i have not included any headers
for my http.get
method.Could this be the problem ?
asp.net-core cors
add a comment |
Hello i am trying to issue a http
get
request to a .NET Core Console App
from my Angular 2
frontend and i get the following error:
Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
For me it is curious since i have enabled CORS
on the server side as you can see below in the Startup
class.
Startup
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();
public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseMvc();
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
I make the request from the UI
like this:
@Injectable()
export class UserService
private static baseUrl:string="http://127.0.0.1:9300/api";
constructor(private http:HttpClient)
getClientsAsync():Promise<User>
let route=UserService.baseUrl+"/getusers";
var data=(this.http.get(route) //should i have some headers here?
.map(resp=>resp)
.catch(err=>
Observable.throwError(err)
) as Observable<User>).toPromise<User>();
return data;
P.S I have tried with Postman
and the request works ,however here in the angular 2
i have not included any headers
for my http.get
method.Could this be the problem ?
asp.net-core cors
add a comment |
Hello i am trying to issue a http
get
request to a .NET Core Console App
from my Angular 2
frontend and i get the following error:
Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
For me it is curious since i have enabled CORS
on the server side as you can see below in the Startup
class.
Startup
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();
public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseMvc();
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
I make the request from the UI
like this:
@Injectable()
export class UserService
private static baseUrl:string="http://127.0.0.1:9300/api";
constructor(private http:HttpClient)
getClientsAsync():Promise<User>
let route=UserService.baseUrl+"/getusers";
var data=(this.http.get(route) //should i have some headers here?
.map(resp=>resp)
.catch(err=>
Observable.throwError(err)
) as Observable<User>).toPromise<User>();
return data;
P.S I have tried with Postman
and the request works ,however here in the angular 2
i have not included any headers
for my http.get
method.Could this be the problem ?
asp.net-core cors
Hello i am trying to issue a http
get
request to a .NET Core Console App
from my Angular 2
frontend and i get the following error:
Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
For me it is curious since i have enabled CORS
on the server side as you can see below in the Startup
class.
Startup
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();
public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseMvc();
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
I make the request from the UI
like this:
@Injectable()
export class UserService
private static baseUrl:string="http://127.0.0.1:9300/api";
constructor(private http:HttpClient)
getClientsAsync():Promise<User>
let route=UserService.baseUrl+"/getusers";
var data=(this.http.get(route) //should i have some headers here?
.map(resp=>resp)
.catch(err=>
Observable.throwError(err)
) as Observable<User>).toPromise<User>();
return data;
P.S I have tried with Postman
and the request works ,however here in the angular 2
i have not included any headers
for my http.get
method.Could this be the problem ?
asp.net-core cors
asp.net-core cors
edited Nov 13 '18 at 8:58
SET
7,79843260
7,79843260
asked Nov 13 '18 at 8:18
Bercovici AdrianBercovici Adrian
1,0801816
1,0801816
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to put UseCors
before UseMvc
.
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
app.UseMvc();
This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276614%2fserver-uses-cors-but-ui-still-gets-allow-origin-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to put UseCors
before UseMvc
.
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
app.UseMvc();
This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.
add a comment |
You need to put UseCors
before UseMvc
.
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
app.UseMvc();
This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.
add a comment |
You need to put UseCors
before UseMvc
.
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
app.UseMvc();
This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.
You need to put UseCors
before UseMvc
.
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
app.UseMvc();
This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.
answered Nov 13 '18 at 8:20
juunasjuunas
21.2k34678
21.2k34678
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276614%2fserver-uses-cors-but-ui-still-gets-allow-origin-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown