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.

In my GeoExt client, I have the following line that retrieves JSON data from a web service into a FeatureStore:

var store = new GeoExt.data.FeatureStore({
        layer: route_layer,
        fields: [
            {name: "length"}
        ],
        proxy: new GeoExt.data.ProtocolProxy({
            protocol: new OpenLayers.Protocol.HTTP({
                url: "./php/pgrouting.php",
                format: new OpenLayers.Format.GeoJSON({
                    internalProjection: epsg_900913,
                    externalProjection: epsg_4326
                })
            })
        })
    });

When this code is executed, Firebug reports the error SyntaxError: JSON.parse: unexpected character, even though the JSON returned by the web service is valid (I've confirmed this against http://jsonlint.com). The response header has been configured accordingly with the line header('Content-type: application/json',true);.

Anyone who might have encountered this issue before, please assist. I've waded around, but no luck yet.

UPDATE: Sample JSON output is at http://pastebin.com/LPLjBDgp. It's valid JSON, but the error SyntaxError: JSON.parse: unexpected character persists. I've even updated my html page by adding the following line <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> in an attempt to restrict character encoding.

share|improve this question
    
Could you share your geojson file? –  drnextgis Sep 3 '12 at 16:30
    
Does your attribute data of the feature contain any unicode (non ascii) characters?; If i remember... in php you have to use: header(....); $response = json_encode($your_data); echo $response; ...which doesnt work if you have non ascii characters. –  U2ros Sep 4 '12 at 8:52
    
That's a possibility. I use json_encode() in the php page, but I've realized I haven't specified character encoding in the html page. I'll restrict character encoding to UTF-8 and get back to you. –  okello Sep 4 '12 at 12:43
    
Or simply test the response with a simple hand constructed json string that doesnt contain any non ascii symbols –  U2ros Sep 4 '12 at 14:11
    
Thanks for the pointer, @U2ros. I'll try that out. –  okello Sep 4 '12 at 14:28

3 Answers 3

Check if you don't have a BOM (Byte Order Mark) in PHP file, this can break JSON.parse while being invisible in browser console.

share|improve this answer

Try it in chrome, it will tell you what the character is when I run your data though something to make it a valid string and try to parse it in chrome I get

message: "Unexpected token ↵"

but you said the new lines aren't in the original so that message doesn't apply to you.

share|improve this answer

JSON provided above looks like pretty print json. Does you get response from web server in such formatting form? JSON.parse works with valid string, but pretty print json - is not valid for JSON.parse.

share|improve this answer
    
Thanks, but I'm not sure I understand what you are saying. The server returns the JSON as a string. When it's pasted in jsonlint, it's jsonlint that introduces the indentation. I also thought it would be easier to read when indented like that. But the server returns a continuous JSON string with no indentation whatsoever. –  okello Sep 6 '12 at 11:03
    
Could you share JSON in its original format. –  drnextgis Sep 6 '12 at 11:05

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.