OpenLayers 从其他站点加载 GML 文件

发布于 2024-11-10 06:24:41 字数 2989 浏览 8 评论 0原文

我在将 GML 文件加载到 OpenLayers 时遇到困难。我已将问题简化为以下内容: - 复制/粘贴示例:http://openlayers.org/dev /examples/behavior-fixed-http-gml.html - 将所有链接替换为绝对链接

这会产生我从本地主机 (file:///) 运行的以下文件/代码:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
        <title>OpenLayers Vector Behavior Example</title>
        <link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css" />
        <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
        <script src="http://openlayers.org/dev/OpenLayers.js"></script>

        <script type="text/javascript">
            var map;

            function init(){
                map = new OpenLayers.Map('map');
                var wms = new OpenLayers.Layer.WMS(
                    "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
                    {layers: 'basic'}
                );

                var layer = new OpenLayers.Layer.Vector("GML", {
                    strategies: [new OpenLayers.Strategy.Fixed()],
                    protocol: new OpenLayers.Protocol.HTTP({
                        url: "http://openlayers.org/dev/examples/gml/polygon.xml",
                        format: new OpenLayers.Format.GML()
                    })
                });

                map.addLayers([wms, layer]);
                map.zoomToExtent(new OpenLayers.Bounds(
                    -3.92, 44.34, 4.87, 49.55
                ));
            }
        </script>
    </head>
    <body onload="init()">
        <h1 id="title">Vector Behavior Example (Fixed/HTTP/GML)</h1>
        <div id="tags">
            vector, strategy, strategies, protocoll, advanced, gml, http, fixed
        </div>
        <p id="shortdesc">

            Vector layer with a Fixed strategy, HTTP protocol, and GML format.
        </p>
        <div id="map" class="smallmap"></div>
        <div id="docs">
            The vector layer shown uses the Fixed strategy, the HTTP protocol,
            and the GML format.
            The Fixed strategy is a simple strategy that fetches features once
            and never re-requests new data.
            The HTTP protocol makes requests using HTTP verbs.  It should be
            constructed with a url that corresponds to a collection of features
            (a resource on some server).
            The GML format is used to serialize features.
        </div>
    </body>
</html>

问题是 GML 未显示(其他一切都很好)。我在控制台中没有错误,但请求状态为 200 OK(在 FF 控制台和 FireBug 网络选项卡中)。 我缺少什么?同源策略失败应该会显示一些错误,不是吗?

I'm having difficulties loading a GML file into OpenLayers. I've stripped down the problem to the following :
- copy/past the example at : http://openlayers.org/dev/examples/behavior-fixed-http-gml.html
- replace all links to absolute

This results in the following file/code that I run from localhost (file:///) :

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
        <title>OpenLayers Vector Behavior Example</title>
        <link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css" />
        <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
        <script src="http://openlayers.org/dev/OpenLayers.js"></script>

        <script type="text/javascript">
            var map;

            function init(){
                map = new OpenLayers.Map('map');
                var wms = new OpenLayers.Layer.WMS(
                    "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
                    {layers: 'basic'}
                );

                var layer = new OpenLayers.Layer.Vector("GML", {
                    strategies: [new OpenLayers.Strategy.Fixed()],
                    protocol: new OpenLayers.Protocol.HTTP({
                        url: "http://openlayers.org/dev/examples/gml/polygon.xml",
                        format: new OpenLayers.Format.GML()
                    })
                });

                map.addLayers([wms, layer]);
                map.zoomToExtent(new OpenLayers.Bounds(
                    -3.92, 44.34, 4.87, 49.55
                ));
            }
        </script>
    </head>
    <body onload="init()">
        <h1 id="title">Vector Behavior Example (Fixed/HTTP/GML)</h1>
        <div id="tags">
            vector, strategy, strategies, protocoll, advanced, gml, http, fixed
        </div>
        <p id="shortdesc">

            Vector layer with a Fixed strategy, HTTP protocol, and GML format.
        </p>
        <div id="map" class="smallmap"></div>
        <div id="docs">
            The vector layer shown uses the Fixed strategy, the HTTP protocol,
            and the GML format.
            The Fixed strategy is a simple strategy that fetches features once
            and never re-requests new data.
            The HTTP protocol makes requests using HTTP verbs.  It should be
            constructed with a url that corresponds to a collection of features
            (a resource on some server).
            The GML format is used to serialize features.
        </div>
    </body>
</html>

The problem is that the GML is not displayed (everything else works great). I have no errors in the console but status 200 OK for the request (both in FF console and FireBug Network tab).
What I am missing? Same origin policy failure should show up some error, no?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-11-17 06:24:41

它确实在 Chrome 中给出了 Access-Control-Allow-Origin 错误。

XMLHttpRequest 无法加载
http://openlayers.org/dev/examples/gml/polygon.xml
来源 http://fiddle.jshell.net 不是
允许的
访问控制允许来源。

对于 FF,它确实给出了您所说的 200 响应代码,但响应不包含任何数据。

http://jsfiddle.net/niklasvh/F76Hp/3/

It does give an Access-Control-Allow-Origin error in Chrome.

XMLHttpRequest cannot load
http://openlayers.org/dev/examples/gml/polygon.xml.
Origin http://fiddle.jshell.net is not
allowed by
Access-Control-Allow-Origin.

For FF it does give a 200 response code as you stated, but the response contains no data.

http://jsfiddle.net/niklasvh/F76Hp/3/

如果没有 2024-11-17 06:24:41

为了保护用户免受脚本向远程服务器发送信息的影响,有一个“同源策略”,该策略规定您只能通过来自与当前正在查看的页面相同的服务器的 HTTP 请求接收数据。

这就解释了为什么wireshark有这些信息,而firebug没有:浏览器不允许传输信息。

JSON-P 是解决这个问题的一个技巧:

http://en.wikipedia.org/wiki/JSONP< /a>

如果您无权访问 JSONP 源或不想要 JSONP 方法,则必须使用代理,该代理会将您的请求转发到远程服务器并返回信息,就好像它来自您的服务器一样自己的服务器。然后,浏览器将无法判断通信最终将发送到远程服务器,并且将允许 HTTP 请求。

As to protect users from scripts sending information to faraway servers, there is a "same origin policy", which states that you can only receive data through HTTP requests from the same server as the page you are currently viewing.

This explains then why wireshark has the information, and firebug doesn't: The browser disallows the transfer of the information.

JSON-P is a trick to get around this:

http://en.wikipedia.org/wiki/JSONP

If you don't have access to a JSONP source or don't want the JSONP approach, you will have to use a proxy, which will forward your requests to the faraway server and return the information as if it came from your own server. The browser will then not be able to tell that the communication is ultimately going to a faraway server and will allow the HTTP requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文