How to read json from remote in javascript [duplicate] - javascript

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 7 years ago.
I have tried to read a json from remote server, then i got this error.
"Cross-Origin Request Blocked: The Same Origin Policy disallows
reading the remote resource"
Then i read that i need to set the datatype to jsonp.
dataType: 'jsonp',
The problem is the json that i am getting isn't in jsonp format, which is required by jsonp. If i don't use jsonp i get the above error message.
How can I resolve this?

You have to enable a CORS request on the server side as well.
Add this line to your code:
response.addHeader("Access-Control-Allow-Origin", "*");
You can replace * with your site's url if you want to allow only for particular domain.

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

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

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);

CORS error on chrome/firefox, but working fine with Internet explorer [duplicate]

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 6 years ago.
I am trying to make ajax call to an alteryx server from a local HTML page. The request I make is working fine and I am getting response on Internet Explorer but does not load on GoogleChrome/Firefox.
Getting 'Access-Control-Allow-Origin' error as:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.
I have tried adding the 'Access-Control-Allow-Origin' to the ajax request header as follows, but this also does not resolves my issue:
$.ajax({
type: 'GET',
url: 'xxxxxxx',
header: {'Access-Control-Allow-Origin': '*'},
data: params,
success: success,
error: error
});
I have traced the request/response header in browser console and could see the custom header under Access-Control-Headers tag.
The data I am receiving is in json format. Also, I cannot make any changes from the server response end as I have no access to it.
I cannot understand why IE is able to load the page and other browsers cannot and if is there any other possible workaround to make it work on other browsers as well.
Thanks,
Aakash
What version of IE?
This SO page may provide insight:
Cross Domain HTTP with Internet Explorer vs Chrome and Firefox
You can try setting the dataType attribute for $.ajax to 'jsonp' for your GET request to get around the issue since you don't have access to the server.

How does the cross-origin policy work? [duplicate]

This question already has answers here:
XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header
(11 answers)
Closed 6 years ago.
This confuses me a lot. Let's say I have 2 tabs open in my browser, one on http://aaa.com and another on http://bbb.com.
Let's say I make a request
$ajax({
method : 'POST',
url : 'http://aaa.com/SomeAction',
...
});
from the JavaScript console of my browser. The way I understand cross-origin policy is that the server only allows that request to happen if the JS console I typed it into was the one in the tab for http://aaa.com. But how does the server know that? Does my browser send it a header that tells it where the request is coming from?
is that the server only allows that request to happen if the JS
console I typed it into was the one in the tab for http://aaa.com
Not true.
Nothing stops example.com from sending an AJAX request to example.org. The Same Origin Policy however will prevent example.com from reading the response returned.
The Same Origin Policy is enforced in the client-side browser, not on the server.

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