CORS ISSUE in Angular2 [duplicate] - javascript

This question already has answers here:
Origin is not allowed by Access-Control-Allow-Origin
(18 answers)
Closed 6 years ago.
I am using webpack angular2 starter kit (https://github.com/AngularClass/angular2-webpack-starter) for my application and now i have an issue.
I try to make get call
getSomeData(): Observable<any> {
let url = here is my api URL;
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8', "Access-Control-Allow-Origin": "*", 'dataType': 'json', });
let options = new RequestOptions({ headers: headers });
return this._http.get(url, options).map(res => res.json());
}
And I have next error
XMLHttpRequest cannot load (my URL) 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:3000' is therefore not allowed access. The response had HTTP status code 405.
This Api (It using firebird) returns XML by default, maybe this cause a problem.
How to make that always came JSON?
How to fix this problem with CORS?

This issue is at the server side. The latter must return a Access-Control-Allow-Origin header in the response of the HTTP call.
Most of time, there are tools you can plug into your server application to do this for you. The server knows that CORS headers must be returned when the client sends a Origin header (automatically added by the browser).
See this article for more details about CORS:
http://restlet.com/blog/2015/12/15/understanding-and-using-cors/

Related

CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource (js/ts) [duplicate]

This question already has answers here:
XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header
(11 answers)
Closed 5 months ago.
I'm trying to use axios to make a post request to my backend. Every time I make the request I was receiving this error:
origin 'http://localhost:1234' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested
resource.
So I added the appropriate header:
const FRONTEND_URL = 'http://localhost:1234'
export async function connect( address:string | undefined, signature:string | undefined ) {
const url = `${SERVER_URL}/connect`;
return await axios.post(
url,
{ address, signature },
{
headers: {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': `${FRONTEND_URL}`,
'content-type': 'application/json',
},
}
);
}
I still get the same error. Why isn't it detecting my Access-Control-Allow-Origin header? (I've restarted servers multiple times)
https://masteringjs.io/tutorials/axios/post-headers
Since you are running the routes in development at it means using various ports you are running into error due to CORS policy.
You may look up to these answers as they are provided already with so much explanation enable cors.
Also, you may look at documentation of CORS package CORS

Axios - No 'Access-Control-Allow-Origin' header is present on the requested resource [duplicate]

This question already has answers here:
CORS error even after setting Access-Control-Allow-Origin or other Access-Control-Allow-* headers on client side
(2 answers)
Closed 1 year ago.
I'm trying to get data from an end-point using VueJS and axios, but I keep getting the following CORS error on this code.
axios
.get(url, {
headers: {
"Access-Control-Allow-Origin": "*",
},
})
.then((response) => {
this.data = response.data;
});
Access to XMLHttpRequest at [url] from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I've looked at multiple answers but the only solutions I'm seeing are using a Chrome extension or changing the server's configuration, which I do not have access to. Is there any way to solve this exclusively through the front-end?
You could use a CORS proxy, like CORS anywhere:
axios
.get("https://cors-anywhere.herokuapp.com/" + url)
.then((response) => {
this.data = response.data;
});
However, this is not a good solution for production, as it relies on an external service to handle your requests, which could lead to issues later down the line.

400 Bad Request for POST to API for js app, but not asp.net mvc app [duplicate]

This question already has answers here:
Axios Http client - How to construct Http Post url with form params
(5 answers)
Closed 3 years ago.
Great explanation on CORS No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
I created a local asp.net mvc app using C# (httpclient) to POST to an API on a server that is set up to allow CORS and it works. But the javascript app I'm running locally returns the following:
Access to XMLHttpRequest at 'https://apiurl/auth/token' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
GET requests work in the js app. Here is a snippet of code:
axios({
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
baseURL: 'https://apiurl',
url: '/oauth/token',
data: {
grant_type: 'password',
username: this.state.username,
password: this.state.password
}
}).then(resp => {
console.log(resp);
});
API is WEBAPI with CORS enabled.
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
I also understand proxying is a potential solution, but I am curious to know if I am missing something here.
Try to set Access-Control-Allow-Origin as https://apiurl, and set withCredentials as true.

How to add Cross Origin Resource Sharing request headers? [duplicate]

This question already has answers here:
XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header
(11 answers)
Closed 4 years ago.
I am building a REST-consumer using React JS, I have build a REST api as the backend service the frontend will be a completly different application. To sepparate the frontend from the backend.
What I am currently struggling with is making a cross origin request from the frontend to the backend. The backend is hosted at http://localhost:8080 and the frontend react app is hosted at http://localhost:3000. When making a request I get the following error on the console:
Failed to load http://localhost:8080/api/something: 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:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Right now I have fixed this error with a chrome extension to allow CORS: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
But this is just a temporary sollution, I have tried adding the Access-Control-Allow-Origin header but that does not seem to work.
This my code used to make the request:
let Myheaders = new Headers()
Myheaders.append("Access-Control-Allow-Origin", "http://localhost:8080/")
let res = await fetch("http://localhost:8080/api/something",
{
method: "get",
mode: "cors",
headers: Myheaders
})
let res1 = await res.json()
I have tried adding multiple values for the Access-Control-Allow-Origin header with backslash or without backslash, but nothing seems to work.
Anyone run into the same issues and have a sollution for it?
this is a backend issue not a frontend
you should allow the cors from the backend and this is depend on the technology that used in the backend

AngularJS No 'Access-Control-Allow-Origin' header [duplicate]

This question already has answers here:
“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL
(17 answers)
Closed 6 years ago.
I have an AngularJS app that I need to post data to a third party URL which is used to store some data on the third party server. I get the following error when I run my code below:
XMLHttpRequest cannot load http://thirdparty.url.com/. 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:51491' is therefore not allowed access.
The code I'm running in my AngularJS factory is:
return $http({
url: '//thirdparty.url.com',
method: "POST",
data: params_string,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
}
});
Cross-Origin Resource sharing(CORS) is a specification that defines the ways for a web server to allow its resources to be accessed by the script running in a web page from a different domain.
The Server and the client work together, using HTTP headers to make accessing cross origin resources possible.
In your case since you browser(client) is chrome/Firefox(and not the older version of IE) , the problem is not with browser.
When you make an ajax call , browser by default will add a request header
Origin: yourdomainname
Your ajax call will only be successful when the server(http://thirdparty.url.com) sends a response similar to below
Access-Control-Allow-Origin: *
In your case , the above response header is not being sent by server.
This means that your http://thirdparty.url.com/
Does not accept requests from external sources that is/are not from http://thirdparty.url.com/, so you have to enable it from your thirdparty.url.com
Access-Control-Allow-Origin header needs to be added in the thirdparty.url.com that you are trying to access and not in your own code. It is for the website to control allowing access to the users, So you can do anything about it from your side.
Add the extension CORS
to your chrome browser.
You can't enable CORS from client side.
I should set at server level.
HTTP access control (CORS)

Categories