3

I'm trying to do a simple post request to use the OGRE web app to convert JSON files to shapefiles.

I wrote this code, but the file doesn't download like it supposed to be, [edit] and it doesn't even upload the json.

The website specifies this as the input request params:

http://ogre.adc4gis.com/convertJson with one of the following params:

json - text of the GeoJSON file
jsonUrl - the URL for a remote GeoJSON file
outputName (optional) - the name for the resulting zip file
skipFailures (optional) - skip failures

What am I doing wrong ?

<html>
<head>
    <script src="http://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript"></script>
</head>

<body>

<input type="button" value="Send Post" onclick="sendPost()">

<script>
var data = { "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "geometry": { "type": "Point", "coordinates": [102.0, 0.5] },
    "properties": { "prop0": "value0" }
  }]
};

function sendPost() {
    $.ajax({
        type: "POST",
        url: 'http://ogre.adc4gis.com/convertJson',
        json: data,
        success: success
    });
}

function success(result) {
    alert('Process achieved!');
}

</script>
</body>
</html>

I get this error:

Object {error: true, msg: "No json provided"}

What is the problem ?

Community
  • 1
  • 1
kaycee
  • 901
  • 1
  • 9
  • 35

2 Answers2

9

there is no such property json for jquery ajax, to add the json as post-data do like this:

function sendPost() {
    $.ajax({
        type: "POST",
        url: 'http://ogre.adc4gis.com/',
        data: {json:JSON.stringify(data)  },
        success: success
    });
}

in your success handler you will have the response in result dont expect your browser to download something as stated in Quentins Answer.

john Smith
  • 17,409
  • 11
  • 76
  • 117
  • If you take a look at the OGRE, it's supposed to take the GeoJSON and convert it to a ZIP shapefile. http://ogre.adc4gis.com/convertJson with one of the following params: json - text of the GeoJSON file jsonUrl - the URL for a remote GeoJSON file outputName (optional) - the name for the resulting zip file skipFailures (optional) - skip failures – kaycee Feb 17 '16 at 14:26
  • i updated the answer, so the json data´s parameter-name is `json` – john Smith Feb 17 '16 at 14:31
  • Thanks, now the request works, but do you know how I shoud handle the result ? It's supposed to download a .ZIP file, but actually I only get strange > icons in the console with the filenames of the files supposed to be in a ZIP between them. – kaycee Feb 17 '16 at 14:36
  • 1
    have a look at this, you could open in browser and then it will download http://stackoverflow.com/questions/23676748/download-zip-file-with-jquery-from-ajax-post-request – john Smith Feb 17 '16 at 14:43
1

but the file doesn't download like it supposed to be.

The file isn't supposed to download.

You have made an Ajax request. The response to it will be handled by JavaScript. It will only be handled by JavaScript. The JavaScript you wrote to handle it just calls alert (and nothing else).

If you want the browser to handle it in the same way as if you had submitted a regular form (without JavaScript) then you should submit a regular form. The whole point of Ajax is to handle the response in a custom way.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335