Issue "XMLHttpRequest cannot load" in Angularjs and WebApi [duplicate] - javascript

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 6 years ago.
I created asp.net webApi and publish in somee.com. I type link xxxx.somee.com/api/xxxx is ok. But I call in Angularjs not run
$http.get('http://xxxxxx.somee.com/api')
.then(function(response) {
console.log(response);
});
I received error:
XMLHttpRequest cannot load xxxx.somee.com/api/xxxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Please give the solutions. Thank you very much.

If your Angular website and API websites are running in a different domain or with ports this issue will happen.
To resolve this please add the following code into your webapiconfig.cs file.
var cors = new EnableCorsAttribute(http(s):// xxxx.somee.com/api/xxxx, "*", "*");
config.EnableCors(cors);

Related

cesium header ‘Access-Control-Allow-Origin’ cannot be set [duplicate]

This question already has answers here:
XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header
(11 answers)
Closed 1 year ago.
I´m trying to access a WMS from statistik.at. Everything is working fine, when i use a browser extension that disables CORS errors, but as soon as I deactivate it i get the following error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at
https://www.statistik.at/gs-inspire/VIEW_PD_POPREG_500M/ows?SERVICE=WMS&VERSION=1.3.0&service=WMS&version=1.1.1&request=GetMap&styles=&format=image%2Fjpeg&layers=PD.StatisticalDistribution&bbox=90%2C0%2C180%2C90&width=256&height=256&srs=EPSG%3A4326.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
So, I´ve tried to set the missing header:
new WebMapServiceImageryProvider({
url: new Resource({
url:
'https://www.statistik.at/gs-inspire/VIEW_PD_POPREG_500M/ows?SERVICE=WMS&VERSION=1.3.0',
headers: {
‘access-control-allow-origin’: 'www.statistik.at',
},
}),
layers: ‘PD.StatisticalDistribution’,
});
But the behaviour of setting access-control-allow-origin is rather unexpectable, because it set´s the following header:
Access-Control-Request-Headers: access-control-allow-origin
which makes every single request invalid…
I hope that someone can help me out! looking forward for any ideas.
Thank you
Maybe this similar question can help you. It works for me(In my case, I was loading with Terrain data using Cesium, where Terrain data was published by Tomcat on the server side. Then I encountered CORS error when accessing Terrain data. This problem solved my error).
https://gis.stackexchange.com/questions/315967/i-cant-see-my-wms-layer-on-cesium-globe-scene

CORS Issues Executing Get Requests Using Axios/Github [duplicate]

This question already has answers here:
from origin 'xxx' has been blocked by CORS policy: [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to authenticate over OAUTH API using Axios. The initial request is just a simple GET to get the auth token.
axios.get(
"https://github.com/login/oauth/authorize?client_id=$ID"
).then((res) => { console.log(res) })
I immediately get:
...from origin 'http://localhost:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I can use an href link and it works totally fine. What could be the issue here?
In simple terms, when you are using the anchor tag, it is a link to the original site. When user click on a tag, user will be redirected to that site. But when an AJAX request user will stay in your site and sends an ajax request to the server(github in this case).
When using HTTP protocol there is a header call origin which will tell the backend server where user is from, see the below picture
So if server does not allow sources other than it self, this security check will be failed and the AJAX request won't be success. Please let me know if you need more clarifications and I'll be glad to help. Hope that helps.

Dude with fetch use [duplicate]

This question already has answers here:
How does the 'Access-Control-Allow-Origin' header work?
(19 answers)
No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
(26 answers)
Closed 4 years ago.
(translated by Google)
Hello
I have a doubt regarding the use of fetch
From my server, I access images that are hosted on another server (such as a CDN)
This piece of code works correctly. The image is shown.
var img = document.getElementById( obj );
img.src = data-src;
// data-src has the url of the image https://serverCDN/image/bar/foo.jpg
However, within the same script, same servers, same image involved, the following code does not work.
Returns the error "Access to fetch at 'https://serverCDN/image/bar/foo.jpg' from origin 'http://myServer' has been blocked by CORS policy: No 'Access -Control-Allow-Origin 'header is present on the requested resource. If an opaque response serves your needs, set the request's mode to' no-cors' to fetch the resource with CORS disabled."
fetch ( data-src ), {})
.then (
function (res) {
console.log (res)
}
);
Because the first piece of code works and the second does not?
I'm confused.
I would be interested in using fetch, because I need to access the RESPONSE HEADERS sent by the CDN server
EDIT (FOR DUPLICATED TAG)
I undestand the CORS concept, but
The Dude is...Why, the first piece of code works and the second does not?
The problem is, that the server on https://servercdn/image/bar/foo.jpg does not send a Cross Origin header. By default, JavaScript XMLHttpRequests (XHR) are bound to the same domain. You can change this behaviour by adding cross origin HTTP header to the target server.
Or a simpler way: Use jsonp: https://www.w3schools.com/js/js_json_jsonp.asp

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

This question already has answers here:
How does the 'Access-Control-Allow-Origin' header work?
(19 answers)
No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
(26 answers)
Closed 4 years ago.
Hi I'm new to javascript and API calls and I'm trying to make an API call using the following javascript code:
var url = 'https://labs.bible.org/api/?
passage=random&type=json&callback=myCallBackFcn';
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', url);
ourRequest.onload = function(){
console.log(ourRequest.responseText);
};
ourRequest.send();
When I refresh my page I get the following error:
Failed to load
https://labs.bible.org/api/?passage=random&type=json&callback=myCallBackFcn:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access.
Can anyone help me with this? Also, can you explain exactly how to fix it?
You have a cross-origin issue, let's explain it.
Browser downloaded js file from example.com website, now js file wants to make ajax to xyz.org website, this is a cross-origin request and so the browser asks xyz.org "do you allow example.com to access your resources"? the browser knows the answer from 'Access-Control-Allow-Origin' Response header.
But xyz.org didn't send this header so the browser assumed xyz.org doesn't want anyone but him to access his resources so the browser just refuses your request.
Note that cross-origin request happens too when you access js file in a URL beginning with file:// and origin here is set to null because js isn't downloaded from any server yet origin null is different from xyz.org.
Solution: if you control xyz.org just make it add Access-Control-Allow-Origin to response headers, you can look the internet for value format for this header.
If it's a third party website you have to call its admins and if they refuse to add the header then you simply have no solution in pure js but hey only browsers respects this policy, if you make a desktop application then it doesn't care and an example of this is Postman the REST APIs tester.

Vue.js - can't load json data using vue-resource [duplicate]

This question already has answers here:
Same origin Policy and CORS (Cross-origin resource sharing)
(2 answers)
Closed 7 years ago.
I'm having trouble using vue-resource with some json data.
I'm following the example here: https://github.com/vuejs/vue-resource/blob/master/docs/http.md
My vue-resource call looks like this:
ready: function() {
this.$http.get('http://section.dynns.com/sections').then(function (response) {
this.$set('data_list', response.data)
});
},
See jsfiddle here:
http://jsfiddle.net/greene48/yb5chmfr/
But, switching that api call for a different set of data seems to work:
http://jsfiddle.net/greene48/92gfwqL3/
So, maybe there is something wrong with the json I'm trying to use?
Any help would be appreciated.
Thanks
http://jsonplaceholder.typicode.com/posts sets CORS headers. http://section.dynns.com/sections does not. Without CORS headers, your requests to a different domain name are subject to the browser's same-origin policy and will fail.
Your other options are JSONP (if the API supports it) or proxying requests through a server-side script.

Categories

Resources