使用angularjs将动态值传递给http.post url

时间:2015-09-17 07:31:58

标签: angularjs elasticsearch angular-ui-router

我正在获取弹性搜索数据,包括索引,id,类型等,如下所示

$http.post('http://localhost:9200/abc/_search', searchCriteria).
                          then(function (response) {
                              $scope.responseData = response;
                          }, function (response) {
                          });

在获取响应后,我试图传递另一个URL中的信息来更新elasticsearch中的信息,如下所示,

$http.post('http://localhost:9200/$scope.responseData._index/$scope.responseData._type/$scope.responseData._id/_update',postData).

我是Angularjs的新手。我不确定这是否有效。如果我错了,请纠正我。

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以验证您的数据是否定义如下:

var url = 'http://localhost:9200/';
var index = $scope.responseData._index || null;
var type = $scope.responseDate._type || null;
var id = $scope.responseData._id || null;

if(index && type && id){
   url = url + index + '/' + type + '/' + id + '/_update';
   $http.post(url, postData);
};

答案 1 :(得分:0)

我只需构建url字符串,然后调用http post:

var url = 'http://localhost:9200/' + $scope.responseData._index + '/' + $scope.responseData._type + '/' + $scope.responseData._id + '/_update';
$http.post(url, postData);
相关问题