传递JSON字符串时的AJAX请求

时间:2014-03-14 22:02:36

标签: ajax json

我有一个代码:

    function fetchFacets() {
    var bounds = map.getBounds();

    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var req ={ match_all: {} };

    var f = 1 - zoom2digits[map.zoom] / 12;
    $.ajax({

        url: "http://146.185.164.121:9200/fnh_main/main/_search?size=0",
        contentType: "text/json",
        type: "POST",
        data: JSON.stringify({
            query: {
                filtered: {
                    query: {
                        req,
                    },
                    filter: {
                        geo_bounding_box: {
                            location : {
                                top_left : ne.lat()+","+sw.lng(),
                                bottom_right : sw.lat()+","+ne.lng()
                                }

                            }
                        }
                    }
                },
    facets : {
        places : { 
            geohash : {
                field : "location",
                factor : f,
                show_geohash_cell: true
            }
        }
    }
}
        ), dataType: "json"}
).done..............

我需要像这样传递它:

filtered: {
                        query: {
                            match_all: {}
                        },

我做错了什么?我得到一个意外的错误,但是当我手动输入match_all:{}时,everithing工作正常。请帮忙!

更新:添加了更多代码。

1 个答案:

答案 0 :(得分:0)

试试这个:

var request = {  match_all: {} };

$.ajax({

    url: "http://146.185.164.121:9200/fnh_main/main/_search?size=0",
    contentType: "text/json",
    type: "POST",
    data: JSON.stringify({
        query: {
            filtered: {
                query:  request,