1

this code works with local kml files, but not when the kml files are on my other server, I got 3 error :

OPTIONS http://192.168.0.150:9000/files/anonymous/AVGHDOP.kml 404 (Not Found) XMLHttpRequest.js:220
OPTIONS http://192.168.0.150:9000/files/anonymous/AVGHDOP.kml Origin http://localhost is not allowed by Access-Control-Allow-Origin. XMLHttpRequest.js:220
XMLHttpRequest cannot load http://192.168.0.150:9000/files/anonymous/AVGHDOP.kml. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

so, I try to put headers in the OpenLayers.Protocol.HTTP. but it still is not working -_-. here is my code:

map = new OpenLayers.Map('map');
osm = new OpenLayers.Layer.OSM();
vectors = new OpenLayers.Layer.Vector("KML", {
 strategies: [new OpenLayers.Strategy.Fixed()],
 protocol: new OpenLayers.Protocol.HTTP({
 url: url_file,//the file is on an other_server(a play server).
 headers: {
 "Access-Control-Allow-Origin":"*",
 "Access-Control-Allow-Headers":"Access-Control-Allow-Origin"},
 format: new OpenLayers.Format.KML({
 extractStyles: true,
 extractAttributes: true,
 maxDepth: 2
 })
 })
});
map.addLayers([osm, vectors]);
map.addControl(new OpenLayers.Control.MousePosition());
map.addControl(new OpenLayers.Control.EditingToolbar(osm));
vectors .events.register("loadend", vectors , function (e) {
 map.zoomToExtent(vectors .getDataExtent());
});

I dont know if I can configure a proxy. So if I can solve this problem without it it would be great :)

Gery
2,2961 gold badge17 silver badges39 bronze badges
asked Jul 18, 2013 at 8:53

1 Answer 1

2

You would solve this using OpenLayers.request methods, check this out:

The above types of request are useful because they are asynchonous. That is, the user can continue to view and interact with the original page while additional data is being requested. A more common way to request data asynchronously is to use the XMLHttpRequst object. With an XMLHttpRequest object, you can open a conection to a server and send or receive data via HTTP (or HTTPS). XMLHttpRequest objects are a bit awkward to use and are unfortunately not supported in all browsers. OpenLayers provides a cross-browser XMLHttpRequest function and wraps it in some convenient OpenLayers.Request methods.

In this link you can find some examples to deal with your issue:

http://docs.openlayers.org/library/request.html

I think with this you can avoid using a proxy, but proxies are not a big deal in OpenLayers, check this out also:

https://collab.itc.virginia.edu/wiki/toolbox/Using%20A%20Proxy%20Script%20With%20Openlayers.html

Hope this helps,

answered Jul 18, 2013 at 9:17
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.