将 POST 请求更改为 GET 以发送 JSON 文件进行缓存

Changing POST Request to GET to Send a JSON File for Caching

我如何修改以下代码,以便它发送包含适当数据的 JSON 文件?

我问的原因是 POST 请求似乎没有被缓存,这对我来说是个问题。我想使用 GET 以便缓存 JSON 文件。基本上这个查询所做的是它获取视口的当前边界并基于该边界从数据库 (MongoDB) 中获取数据。

我尝试在关闭追加之前添加 + ".json",但它不起作用。

myJSON.append({"type":"Feature","geometry":obj["geometry"],"properties":{"Address":obj["properties"]["Address"],"ID":obj["properties"]["ID"]}})

完整代码如下:

@app.route('/getData', methods=['POST','GET'])
def getData():
    myCollection = db["myCollection"]
    bBox = [json.loads(request.data)]
    myJSON = []

    for obj in myCollection.find({"geometry":{"$geoWithin": {"$geometry": {"type" : "Polygon" ,"coordinates": bBox}}}},{"properties.Address":1,"properties.ID":1,"geometry":1}):
        myJSON.append({"type":"Feature","geometry":obj["geometry"],"properties":{"Address":obj["properties"]["Address"],"ID":obj["properties"]["ID"]}})

    return jsonify(data = {"type":"FeatureCollection", "features":myJSON})

在客户端,AJAX 请求看起来像这样:

bounds = getBoundingBox();
$.ajax({
    url: '/getData',
    type: 'POST',
    data: JSON.stringify(bounds),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
        // add data here
    }
});

您需要发送 JSON 数据作为 GET 参数并且它需要一个名称

$.ajax({
    url: '/getData',
    type: 'GET',
    data: {bounds: JSON.stringify(bounds)},
    success: function (data) {
        // add data here
    }
});

然后在服务器端,检索 bounds GET 参数并将其用于 json.loads 函数。