Given JSON like this:

HttpListener listener = new HttpListener(); listener.Prefixes.Add(new Uri("http://localhost:8181")); listener.Start(); while (isListening) { HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; //... MemoryStream ms = (GeoJsonDeserialize.SerializeGeoJson(ObjToJson) as MemoryStream); byte[] buffer = ms.ToArray(); response.StatusCode = (int)HttpStatusCode.OK; response.KeepAlive = false; response.ContentType = "application/json"; response.ContentEncoding = System.Text.UTF8Encoding.UTF8; response.ContentLength64 = buffer.Length; response.AddHeader("Cache-Control", "private, max-age=0"); Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); ms = new MemoryStream(); ms.Write(buffer, 0, buffer.Length); buffer = ms.ToArray(); output.Close(); } 

In another C # program, I request this:

 WebRequest webRequest = WebRequest.Create("http://localhost:8181"); WebResponse webResponse = webRequest.GetResponse(); StreamReader sr = new StreamReader(webResponse.GetResponseStream()); String str = sr.ReadToEnd(); 

and see the transferred text ( JSON ).
But the trouble is - neither Ajax sees the response body (gives an error), nor OpenLayers, which according to GeoJSON should show points on the map.

OpenLayers requests data ( OpenLayersRotationExample is taken as a basis):

 vectors = new OpenLayers.Layer.Vector( "Simple Geometry", { protocol: new OpenLayers.Protocol.HTTP({ url: "http://localhost:8181", format: new OpenLayers.Format.GeoJSON() }), strategies: [new OpenLayers.Strategy.Fixed()] }); map.addLayers([vectors]); 

An object in JSON ( GeoJSON ) looks like this:


Specially tried the text of the transmitted JSON to load into OL directly in JavaScript:

 var featurecollection = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-111.04,45.68]},"properties":{"course":42.9}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-111.04,45.68]},"properties":{"course":157.5}}]}; var geojson_format = new OpenLayers.Format.GeoJSON(); var vector_layer = new OpenLayers.Layer.Vector("Simple 1", { styleMap: new OpenLayers.StyleMap({ "default": { externalGraphic: "OpenLayersRotationExample_files/marker-gold.png", //graphicWidth: 17, graphicHeight: 20, graphicYOffset: -19, rotation: "${course}", }, "select": { cursor: "crosshair", externalGraphic: "OpenLayersRotationExample_files/marker.png" } }) }); map.addLayer(vector_layer); vector_layer.addFeatures(; 

And everything works - the points are displayed.
Conclusion - I give wrong JSON.
Tell me how, in response to a request, give JSON so that JavaScript can accept it?

  • If I understand correctly, the text from the server comes to you. Accordingly, it must be converted to an object. You can eval'om, and you can connect the library to work with json'om. - ling
  • The trouble is that JS accepts nothing at all - that's why I ask that I don't understand what's the matter (after all, the whole thing starts locally - the HTML page and the application in C #). - t1nk

1 answer 1

Since the provider of points and the page displaying the map are located in different domains, the Same Orign Policy operates.

OpenLayers has OpenLayers.Protocol.Script that uses JSONP to get data. By default, the callback is called OpenLayers.Protocol.Script.registry.c1 . You need to remake the provider accordingly so that it wraps the resulting JSON into this function.

  • Thanks, did this: vectors = new OpenLayers.Layer.Vector (Simple Geometry ", {protocol: new OpenLayers.Protocol.Script ({url:" localhost: 8181 ", callbackKey:" format_options ", callbackPrefix:" callback: ", filterToParams: function (filter, params) {if (filter.type === OpenLayers.Filter.Spatial.BBOX) {params.bbox = filter.value.toArray (); if (filter.projection) {params.bbox.push (filter.projection.getCode ());}} return params;}})}); It remains to figure out what he wrote ... - t1nk
  • one
    This is enough for you: protocol: new OpenLayers.Protocol.Script ({url: " localhost: 8181 ", format: new OpenLayers.Format.GeoJSON ()}) The request to the source will look like this: localhost: 8181 /? Callback = OpenLayers .Protocol.Script.registry.c1 And the response should contain the function passed in the callback parameter: OpenLayers.Protocol.Script.registry.c1 ({"type": "FeatureCollection", ......}); - mantigatos
  • And why can the Uncaught TypeError: Object # <Object> has no method 'c1' error fall out due to which the update strategy does not work, in other words, the points on the map do not update their position, being output once? - t1nk
  • one
    Well, for example, due to the fact that there is no such function for an object. When using OpenLayers.Strategy.Refresh callback is different for each request, so keep this in mind when writing your JSONP provider. - mantigatos