使用Ajax进行简单的POST请求 [英] Simple POST request with Ajax
问题描述
我正在尝试做一个简单的发布请求,以使用 OGRE网络应用将JSON文件转换为shapefile .
I'm trying to do a simple post request to use the OGRE web app to convert JSON files to shapefiles.
我编写了这段代码,但是文件没有按原样下载,[edit],甚至都没有上传json.
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
我在做什么错了?
<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>
我收到此错误:
Object {error: true, msg: "No json provided"}
出什么问题了?
推荐答案
jquery ajax没有这样的属性json
,
将json添加为后数据的方法如下:
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
});
}
在成功处理程序中,您将在result
中得到响应
不要期望您的浏览器会下载Quentins答案中所述的内容.
in your success handler you will have the response in result
dont expect your browser to download something as stated in Quentins Answer.
这篇关于使用Ajax进行简单的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!