Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have the following GeoJSON (from webservice)

{
"Features": [
    {
        "Id": "1",
        "Geometry": {
            "Coordinates": [
                {
                    "Latitude": 31.2812,
                    "Longitude": 34.8027,
                    "Altitude": 0
                }
            ],
            "Type": 0,
            "CRS": null,
            "BoundingBoxes": null
        },
        "Properties": {
            "Color": "#00FF55"
        },
        "Type": 7,
        "CRS": null,
        "BoundingBoxes": null
    },
    {
        "Id": "2",
        "Geometry": {
            "Coordinates": [
                {
                    "Latitude": 31.7825,
                    "Longitude": 35.1966,
                    "Altitude": 0
                }
            ],
            "Type": 0,
            "CRS": null,
            "BoundingBoxes": null
        },
        "Properties": {
            "Color": "#FF5500"
        },
        "Type": 7,
        "CRS": null,
        "BoundingBoxes": null
    }
],
"Type": 8,
"CRS": null,
"BoundingBoxes": null

}

This is the webservice code :

public string GetItemsFromDBGeoJSON()
{
    List<GeoJSON.Net.Feature.Feature> fList = new List<GeoJSON.Net.Feature.Feature>();
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("dbo.GetGPSData", conn))
        {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                string name = (string)reader["GPSUserName"];
                double lon = Convert.ToDouble(((decimal)reader["Lon"]));
                double lat = Convert.ToDouble(((decimal)reader["Lat"]));
                string clr = (string)reader["GPSUserColor"];

                GeoJSON.Net.Geometry.GeographicPosition latlon = new GeoJSON.Net.Geometry.GeographicPosition(lat,lon,0);
                GeoJSON.Net.Geometry.Point point = new GeoJSON.Net.Geometry.Point(latlon);                    

                Dictionary<string,object> properties = new Dictionary<string,object>();
                properties.Add("Color", clr);
                GeoJSON.Net.Feature.Feature feature = new GeoJSON.Net.Feature.Feature(point, properties);
                feature.Id = name;                    

                fList.Add(feature);
            }
            conn.Close();
        }
    }
    GeoJSON.Net.Feature.FeatureCollection fCol = new GeoJSON.Net.Feature.FeatureCollection(fList);
    JavaScriptSerializer serializer = new JavaScriptSerializer();

    return serializer.Serialize(fCol);
}

The Ajax call gives me error :

SyntaxError: JSON.parse: unexpected character

Here is the request using JQuery

$.ajax({
    type: "POST",
    url: "http://localhost:36361/Service.asmx/GetItemsFromDBGeoJSON",
    dataType: 'json',
    success: function (response) {
        console.log('success');
    },
    error: function (req, status, error) {
        console.log(error);
    }
});

Also , Using Firebug Console , I see this response from the request:

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">{"Features":[{"Id":"1","Geometry":{"Coordinates":[{"Latitude":31.2812,"Longitude":34.8027,"Altitude":0}],"Type":0,"CRS":null,"BoundingBoxes":null},"Properties":{"Color":"#00FF55"},"Type":7,"CRS":null,"BoundingBoxes":null},{"Id":"2","Geometry":{"Coordinates":[{"Latitude":31.7825,"Longitude":35.1966,"Altitude":0}],"Type":0,"CRS":null,"BoundingBoxes":null},"Properties":{"Color":"#FF5500"},"Type":7,"CRS":null,"BoundingBoxes":null}],"Type":8,"CRS":null,"BoundingBoxes":null}</string>

Any idea why ? I want to show it on leaflet layer.

share|improve this question
1  
could you post your jquery code? –  simo Jul 12 '13 at 10:27
    
@Simo : Edited my post with the jquery code. –  Alophind Jul 12 '13 at 10:32
    
Where does this geojson come from ? The syntax is really strange, e.g. '"Type": 8' should be something like '"Type":FeatureCollection'.Try cleaning it with geojsonlint.com, remove the upper case lettre, etc –  FredB Jul 12 '13 at 10:36
    
@FredB : I'm using GeoJSON.net , I'll add that code also. –  Alophind Jul 12 '13 at 10:36
    
@FredB : I've added the code , even if my geojson not valid , I don't understand why the json itself gives me bad result. –  Alophind Jul 12 '13 at 10:42
add comment

1 Answer

up vote 2 down vote accepted

Well, from firebug console, it seems the returned content is not pure json but a mix of xml and json ....

Be sure the url passed in ajax call return pure json and it will work fine.

share|improve this answer
    
I've added "contentType: "application/json; charset=utf-8;"," and now it works. –  Alophind Jul 12 '13 at 12:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.