在jQuery ajax中调用post给出错误

时间:2014-09-20 07:29:20

标签: jquery ajax

我正在尝试使用POST方法从JQuery ajax调用rest服务。以下是我的代码:

<!DOCTYPE html>
<html>
<head>
<script src="lib/js/jquery/jquery-1.7.2.min.js"></script>
</head>
<body>
    <div id="div1">
        <h2>Let jQuery AJAX Change This Text</h2>
    </div>
    <button id="totalRevenue">Get External Content</button>

    <script>
function jsonpCallback(response) {
    console.log("response from server: "+JSON.stringify(response));
    };

$(document).ready(function() {
    $("#totalRevenue").click(function() {
            console.log('inside function');
            var input = {

                      "facets": {

                        "stats": {

                          "statistical": {

                            "field": "@fields.TransTotalAmount"

                          },

                          "facet_filter": {

                            "fquery": {

                              "query": {

                                "filtered": {

                                  "query": {

                                    "bool": {

                                      "should": [

                                        {

                                          "query_string": {

                                            "query": "*"

                                          }

                                        }

                                      ]

                                    }

                                  },

                                  "filter": {

                                    "bool": {

                                      "must": [

                                        {

                                          "range": {

                                            "@timestamp": {

                                              "from": 1411136110765 ,

                                              "to": "now"

                                            }

                                          }

                                        }

                                      ]

                                    }

                                  }

                                }

                              }

                            }

                          }

                        },

                        "stats_All Transactions": {

                          "statistical": {

                            "field": "@fields.TransTotalAmount"

                          },

                          "facet_filter": {

                            "fquery": {

                              "query": {

                                "filtered": {

                                  "query": {

                                    "bool": {

                                      "should": [

                                        {

                                          "query_string": {

                                            "query": "*"

                                          }

                                        }

                                      ]

                                    }

                                  },

                                  "filter": {

                                    "bool": {

                                      "must": [

                                        {

                                          "range": {

                                            "@timestamp": {

                                              "from": 1411136110765 ,

                                              "to": "now"

                                            }

                                          }

                                        }

                                      ]

                                    }

                                  }

                                }

                              }

                            }

                          }

                        }

                      },

                      "size": 0

                    };

        $.ajax( {
                 type:"POST",
                 url:"http://esu2v977:9200/labs-uat-2014-09-19/_search",
                 dataType:"jsonp",
                 contentType:"application/javascript; charset=UTF-8",
                 data:JSON.stringify(input),
                 successs:jsonpCallback,
                  error: function(data) {
                   console.log("there is some problem to get the response :"  
                                 +JSON.stringify(data));
                }
            }); 
    });

});
</script>
</body>
</html>

它没有在firefox浏览器控制台中返回任何响应。我试过谷歌浏览器,在网络选项卡下它显示一些错误,我没有得到它。请帮帮我。

1 个答案:

答案 0 :(得分:0)

你不能使用javascript做一个跨源的ajax POST请求。看看这篇文章 Cross domain ajax request