Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
ASP.NET
<%@ 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> 

When i click on the map marker points in chrome I am getting the below Error
Error:
"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."

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
XML
<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.
Posted
Updated 1-Sep-14 19:58pm
v11
Comments
Kornfeld Eliyahu Peter 1-Sep-14 1:43am    
If I understand it right localhost and 10.81.5.31 are the same web server - try to use only one of them in your test...
D-Kishore 1-Sep-14 2:31am    
Yes you are right both IIS and geoserver are in the same system, how can i use only one of them. Please guide me.
I am calling geoserver wms layer in my web application. it works fine in IE.

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.
 
Share this answer
 
Yes I did it,

Created one generic handler for my project named as xDomainProxy.ashx

and add the below code
C#
using System;
using System.Web;
 
public class xDomainProxy : IHttpHandler {
     
    public void ProcessRequest (HttpContext context) {
 
        //OpenLayers.ProxyHost = 'xdomainProxy.aspx?url=' so the requested url is passed in a url param
        string requestUrl = HttpUtility.UrlDecode(context.Request.QueryString["url"]);
 
        //test if the request string was passed and of so request data from the destination server
        if (requestUrl != null)
        {
            //create a new HttpWebRequest
            System.Net.HttpWebRequest webRequest;
            webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(requestUrl);
            webRequest.Method = "GET";
 
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse();
 
            //check if the data was successfully retrieved
            if (response.StatusCode.ToString().ToLower() == "ok")
            {
                //set the appropriate response content type
                context.Response.ContentType = response.ContentType;
 
                //Read the stream associated with the response.
                System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
 
                //and write it to response
                context.Response.Write(reader.ReadToEnd());
            }
        }
    }
  
    public bool IsReusable {
        get {
            return false;
        }
    }
}


then called in my javascript code
setting up a proxyHost in OpenLayers:
JavaScript
//proxy host
OpenLayers.ProxyHost = 'xDomainProxy.ashx?url=';
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900