Fórum XMLHttpRequest cannot load http://localhost:8080/featureCollection.geojson. No Access-Control-Allow-Origin header is present on the requested resource. Origin null is therefore not allowed access #476319
15/04/2014
0
Estou usando Open layers para exibir mapas web. Tenho um mapa em geojson extraído do postgis no meu diretório www do servidor de mapas.
Quando abro a página está dando o erro: XMLHttpRequest cannot load http://localhost:8080/featureCollection.geojson. No ''''Access-Control-Allow-Origin'''' header is present on the requested resource. Origin ''''null'''' is therefore not allowed access.
Li a respeito.. entendi que preciso criar um objeto XMLHttpRequest para receber meu arquivo e depois usá-lo no código. Mas estou executando tudo localmente a página e o arquivo estão no mesmo servidor que é a minha máquina.
Alguém pode me dar um exemplo de como devo tratar isso?
Meu código está assim:
Quando abro a página está dando o erro: XMLHttpRequest cannot load http://localhost:8080/featureCollection.geojson. No ''''Access-Control-Allow-Origin'''' header is present on the requested resource. Origin ''''null'''' is therefore not allowed access.
Li a respeito.. entendi que preciso criar um objeto XMLHttpRequest para receber meu arquivo e depois usá-lo no código. Mas estou executando tudo localmente a página e o arquivo estão no mesmo servidor que é a minha máquina.
Alguém pode me dar um exemplo de como devo tratar isso?
Meu código está assim:
<!DOCTYPE html>
<html lang=''''en''''>
<head>
<meta charset=''''utf-8'''' />
<title>My OpenLayers Map</title>
<script type=''''text/javascript'''' src=''''OpenLayers.js''''></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type=''''text/javascript''''>
var map;
function geraMapa() {
//cria o mapa
map = new OpenLayers.Map(''''map_element'''', {});
//adiocna a camada GeoJson Layer para receber o geojson
vectorLayer = new OpenLayers.Layer.Vector("GeoJsonLayer");
map.addLayer(vectorLayer);
//Função para ler o arquivo
function handler(request) {
var geojson_format = new OpenLayers.Format.GeoJSON({
''''internalProjection'''': map.baseLayer.projection,
''''externalProjection'''': new OpenLayers.Projection("EPSG:4326")
});
vectorLayer.addFeatures(geojson_format.read(request.responseText));
}
var request = OpenLayers.Request.GET({
url: "featureCollection.geojson",
callback: handler
});
//Adicona o switcher ao mapa
map.addControl(new OpenLayers.Control.LayerSwitcher({}));
//Centraliza o mapa
if(!map.getCenter()){
map.zoomToMaxExtent();
}
}
</script>
</head>
<body onload=''''geraMapa();''''>
<div id=''''map_element'''' style=''''width: 1000px; height: 900px;''''>
</div>
</body>
</html>
Cássia Freitas
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)