我收到错误没有'Access-Control-Allow-Origin'标头出现在请求的资源上。因此不允许Origin'http:// localhost'访问。 [英] I am getting error No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

查看:134
本文介绍了我收到错误没有'Access-Control-Allow-Origin'标头出现在请求的资源上。因此不允许Origin'http:// localhost'访问。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7操作系统中使用vs2010。

我安装了Tomcat-7.0.27然后我将geoserver war文件复制到其中。



这里Tomcat和geoserver正常运行(1.Tomcat:http:// localhost:8085

2.Geoserver:http:// localhost:8085 / geoserver / index.html)



然后我在geoserver中创建了一个wms图层。工作正常。



我在我的网络应用程序中调用了这个wms层。

I am using vs2010 in windows 7 OS.
I installed Tomcat-7.0.27 then i copied geoserver war file into it.

here Tomcat and geoserver is running fine(1.Tomcat: http://localhost:8085
2.Geoserver: http://localhost:8085/geoserver/index.html)

Then i created one wms layer in geoserver. its working fine.

I am calling the this wms layer in my web application.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Maps_Google.WebForm5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css" type="text/css" /> 
    <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" /> 
    <script type="text/javascript" src="http://openlayers.org/dev/OpenLayers.js"></script> 
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript">

        var map;

        function init() {

            var options = {
                maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
                maxResolution: "auto",
                projection: new OpenLayers.Projection("EPSG:900913"),
                controls: [],
                units: 'm'
            };

            map = new OpenLayers.Map('map', options);

            map.addControl(new OpenLayers.Control.LayerSwitcher());

            var gsat = new OpenLayers.Layer.Google(
        "Google Physical",
        { type: google.maps.MapTypeId.TERRAIN, numZoomLevels: 22 }
    );

            var NE_image = new OpenLayers.Layer.WMS(
                    "cite:vietnam_IZ_V4 - Untiled", "http://10.81.5.31:8085/geoserver/cite/wms?", { LAYERS: 'cite:vietnam_IZ_V4', transparent: true }, { isBaseLayer: false }
                );

                      
            map.addLayers([gsat, NE_image]);

            // Google.v3 uses EPSG:900913 as projection, so we have to 
            // transform our coordinates 
//            map.setCenter(new OpenLayers.LonLat(0,0).transform(
//        new OpenLayers.Projection("EPSG:4326"),
//        map.getProjectionObject()
            //    ), 0);
            map.setCenter(new OpenLayers.LonLat(0, 0), 0);
            //map.zoomToMaxExtent();

            var bounds = new OpenLayers.Bounds(
                    103.98606273474692, 8.775560097336232,
                    109.40715417511227, 22.48734509850342
                );
            // build up all controls
            map.addControl(new OpenLayers.Control.PanZoomBar({
                position: new OpenLayers.Pixel(2, 15)
            }));
            map.addControl(new OpenLayers.Control.Navigation());
            map.addControl(new OpenLayers.Control.Scale($('scale')));
            map.addControl(new OpenLayers.Control.MousePosition({ element: $('location') }));
            format = 'image/png';

            //map.zoomToExtent(bounds);
            // support GetFeatureInfo
            map.events.register('click', map, function (e) {
                document.getElementById('nodelist').innerHTML = "Loading... please wait...";
                var params = {
                    REQUEST: "GetFeatureInfo",
                    EXCEPTIONS: "application/vnd.ogc.se_xml",
                    BBOX: map.getExtent().toBBOX(),
                    SERVICE: "WMS",
                    INFO_FORMAT: 'text/html',
                    QUERY_LAYERS: map.layers[1].params.LAYERS,
                    FEATURE_COUNT: 50,
                    "Layers": 'cite:vietnam_IZ_V4',
                    WIDTH: map.size.w,
                    HEIGHT: map.size.h,
                    format: format,
                    styles: map.layers[1].params.STYLES,
                    srs: map.layers[1].params.SRS
                };

                // handle the wms 1.3 vs wms 1.1 madness
                if (map.layers[1].params.VERSION == "1.3.0") {
                    params.version = "1.3.0";
                    params.j = parseInt(e.xy.x);
                    params.i = parseInt(e.xy.y);
                } else {
                    params.version = "1.1.1";
                    params.x = parseInt(e.xy.x);
                    params.y = parseInt(e.xy.y);
                }

                // merge filters
                if (map.layers[1].params.CQL_FILTER != null) {
                    params.cql_filter = map.layers[1].params.CQL_FILTER;
                }
                if (map.layers[1].params.FILTER != null) {
                    params.filter = map.layers[1].params.FILTER;
                }
                if (map.layers[1].params.FEATUREID) {
                    params.featureid = map.layers[1].params.FEATUREID;
                }
                //OpenLayers.ProxyHost = "C:\\Users\\kdonipar1\\Downloads\\proxy.cgi?url=";
                OpenLayers.loadURL("http://10.81.5.31:8085/geoserver/cite/wms", params, this, setHTML, setHTML);
                OpenLayers.Event.stop(e);
            });
        }
        function setHTML(response) {
            document.getElementById('nodelist').innerHTML = response.responseText;
        };
    </script> 
  </head> 

  <body onload="init()"> 
    <h1 id="title">OpenLayers: Google Layer Example</h1> 
    <div id="map" class="smallmap" style="width: 100%; height: 500px;"></div> 
    <div id="nodelist">
            Click on the map to get feature info
        </div>
  </body> 

</html> 



当我点击chrome中的地图标记点时,我得到了以下错误


When i click on the map marker points in chrome I am getting the below Error

错误:

XMLHttpRequest无法加载http://10.81.5.31:8085/geoserver/cite/wms?REQUEST=GetFeatureInfo& EXCEPTIONS ... 00安培;格式=图像%2Fpng&安培;样式=安培; SRS = EPSG%3A900913&安培;版本1.1.1 =&安培; X = 1226&安培; Y = 116。请求的资源上不存在Access-Control-Allow-Origin标头。原因'http:// localhost'因此不允许访问。

"XMLHttpRequest cannot load http://10.81.5.31:8085/geoserver/cite/wms?REQUEST=GetFeatureInfo&EXCEPTIONS…00&format=image%2Fpng&styles=&srs=EPSG%3A900913&version=1.1.1&x=1226&y=116. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access."



此错误仅出现在chrome中。

它在IE中工作正常,并且它在我点击标记时显示地图标记信息。



后来在谷歌搜索我修复了它但添加了谷歌浏览器的快捷方式: - disable-web -security。

但这不是一个可行的解决方案。



我尝试添加web.config文件


This error coming only in the chrome.
Its works fine in IE, and Its displaying the map marker information when I click on the marker.

Later searching in google I fixed it but adding in the shortcut of google chrome : --disable-web-security.
But this is not a feasible solution.

I tried adding web.config file

<system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
    <httpProtocol>
      <customHeaders>
        <!-- Enable Cross Domain AJAX calls -->
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>





这也不成功。

请给我你的建议如何解决这个问题。



This is also not successful.
Please give me your suggestions how do I can overcome this problem.

推荐答案

' < span class =code-string> scale')));
map.addControl( new OpenLayers.Control.MousePosition({element:
('scale'))); map.addControl(new OpenLayers.Control.MousePosition({ element:


' location')}));
format = ' image / png';

// map.zoomToExtent(bounds);
< span class =code-comment> // 支持GetFeatureInfo
map.events.register(' 点击,地图,功能(e){
document .getElementById(' nodelist')。innerHTML = 正在加载......请稍候......;
var params = {
REQUEST: GetFeatureInfo
EXCEPTIONS: application / vnd.ogc.se_xml
BBOX:map.getExtent()。toBBOX(),
SERVICE: WMS
INFO_FORMAT:' text / html'
QUERY_LAYERS:map.layers [ 1 ]。params.LAYERS,
FEATURE_COUNT: 50
图层' cite:vietnam_IZ_V4'
WIDTH:map.size.w,
HEIGHT:map.size.h,
格式:格式,
样式:map.layers [ 1 ]。params.STYLES,
srs:map.layers [ 1 ]。params.SRS
};

// 处理wms 1.3 vs wms 1.1 madness
if (map.layers [ 1 ]。params.VERSION == 1.3.0){
params.version = 1.3.0;
params.j = parseInt (e.xy.x);
params.i = parseInt (e.xy.y);
} else {
params.version = 1.1.1;
params.x = parseInt (e.xy.x);
params.y = parseInt (e.xy.y);
}

// 合并过滤器
< span class =code-keyword> if
(map.layers [ 1 ]。params.CQL_FILTER!= null ){
params.cql_filter = map.layers [ 1 ]。params.CQL_FILTER;
}
if (map.layers [ 1 ]。params.FILTER! = null ){
params.filter = map.layers [ 1 ]。params.FILTER ;
}
if (map.layers [ 1 ]。params.FEATUREID) {
params.featureid = map.layers [ 1 ]。params.FEATUREID;
}
// OpenLayers.ProxyHost =C:\\Users \\\ \\ kdonipar1 \\Downloads\\proxy.cgi?url =;
OpenLayers.loadURL( http://10.81.5.31:8085/geoserver/cite/wms,params, this ,setHTML,setHTML);
OpenLayers.Event.stop(e);
});
}
function setHTML(response){
document .getElementById (' nodelist')。innerHTML = response.responseText;
};
< / script >
< / head >

< body onload = init() >
< h1 id = title > OpenLayers: Google图层示例< / h1 >
< div id = map class = smallmap style = width:100%;高度:500px; > < / div >
< div id = nodelist >
点击地图获取相关信息
< / div >
< / body >

< / html >
('location') })); format = 'image/png'; //map.zoomToExtent(bounds); // support GetFeatureInfo map.events.register('click', map, function (e) { document.getElementById('nodelist').innerHTML = "Loading... please wait..."; var params = { REQUEST: "GetFeatureInfo", EXCEPTIONS: "application/vnd.ogc.se_xml", BBOX: map.getExtent().toBBOX(), SERVICE: "WMS", INFO_FORMAT: 'text/html', QUERY_LAYERS: map.layers[1].params.LAYERS, FEATURE_COUNT: 50, "Layers": 'cite:vietnam_IZ_V4', WIDTH: map.size.w, HEIGHT: map.size.h, format: format, styles: map.layers[1].params.STYLES, srs: map.layers[1].params.SRS }; // handle the wms 1.3 vs wms 1.1 madness if (map.layers[1].params.VERSION == "1.3.0") { params.version = "1.3.0"; params.j = parseInt(e.xy.x); params.i = parseInt(e.xy.y); } else { params.version = "1.1.1"; params.x = parseInt(e.xy.x); params.y = parseInt(e.xy.y); } // merge filters if (map.layers[1].params.CQL_FILTER != null) { params.cql_filter = map.layers[1].params.CQL_FILTER; } if (map.layers[1].params.FILTER != null) { params.filter = map.layers[1].params.FILTER; } if (map.layers[1].params.FEATUREID) { params.featureid = map.layers[1].params.FEATUREID; } //OpenLayers.ProxyHost = "C:\\Users\\kdonipar1\\Downloads\\proxy.cgi?url="; OpenLayers.loadURL("http://10.81.5.31:8085/geoserver/cite/wms", params, this, setHTML, setHTML); OpenLayers.Event.stop(e); }); } function setHTML(response) { document.getElementById('nodelist').innerHTML = response.responseText; }; </script> </head> <body onload="init()"> <h1 id="title">OpenLayers: Google Layer Example</h1> <div id="map" class="smallmap" style="width: 100%; height: 500px;"></div> <div id="nodelist"> Click on the map to get feature info </div> </body> </html>



当我点击chrome中的地图标记点时,我得到了以下错误


When i click on the map marker points in chrome I am getting the below Error

错误:

XMLHttpRequest无法加载http://10.81.5.31:8085/geoserver/cite/wms?REQUEST=GetFeatureInfo& EXCEPTIONS ... 00安培;格式=图像%2Fpng&安培;样式=安培; SRS = EPSG%3A900913&安培;版本1.1.1 =&安培; X = 1226&安培; Y = 116。请求的资源上不存在Access-Control-Allow-Origin标头。原因'http:// localhost'因此不允许访问。

"XMLHttpRequest cannot load http://10.81.5.31:8085/geoserver/cite/wms?REQUEST=GetFeatureInfo&EXCEPTIONS…00&format=image%2Fpng&styles=&srs=EPSG%3A900913&version=1.1.1&x=1226&y=116. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access."



此错误仅出现在chrome中。

它在IE中工作正常,并且它在我点击标记时显示地图标记信息。



后来在谷歌搜索我修复了它但添加了谷歌浏览器的快捷方式: - disable-web -security。

但这不是一个可行的解决方案。



我尝试添加web.config文件


This error coming only in the chrome.
Its works fine in IE, and Its displaying the map marker information when I click on the marker.

Later searching in google I fixed it but adding in the shortcut of google chrome : --disable-web-security.
But this is not a feasible solution.

I tried adding web.config file

<system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
    <httpProtocol>
      <customHeaders>
        <!-- Enable Cross Domain AJAX calls -->
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>





This is also not successful.

Please give me your suggestions how do I can overcome this problem.



This is also not successful.
Please give me your suggestions how do I can overcome this problem.


you might need to allow cross domain requests



[try this]



there is some perticular xml file that when included in your website folder allows that, couldn’t find that.
you might need to allow cross domain requests

[try this]

there is some perticular xml file that when included in your website folder allows that, couldn't find that.


这篇关于我收到错误没有'Access-Control-Allow-Origin'标头出现在请求的资源上。因此不允许Origin'http:// localhost'访问。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆