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:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-108.04,44.68]},"properties":{"course":184.7}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-111.04,45.68]},"properties":{"course":44.1}}]}
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(geojson_format.read(featurecollection));
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?