简体   繁体   English

Angular:将curl转换为Angular $ http POST请求

[英]Angular: Convert curl to Angular $http POST request

I have this curl line: 我有这个卷曲线:

curl -X POST -H "content-type: application/json" -H "AUTH_TOKEN: vLC4grMuKbtbamJL3L6x" localhost:8080/v1/segments?appkey=c2439fb30a1cad03e9e02bd733ef2ad5 -d '{"segment" : {"segment_name" : "ャロットケーキが好き", "segment_type":"static"}}'

Now that curl directly hits the API. 现在curl直接命中了API。 I'd like to be able to do a $http POST after the save and redirection back to route 'return region.$save((function() {': 我希望能够在保存和重定向回到路由'返回区域后执行$ http POST。$ save((function(){':

'use strict';
angular.module('otherLevelsApp').controller('GeoRegionCreateCtrl', [
'$scope', '$location', 'GeoRegion', 'Hours', '$http', function($scope, $location, GeoRegion, Hours, $http) {
console.log('new region controller');
$scope.app_id = (/apps\/(\d+)/.exec($location.absUrl())[1]);
$scope.newGeoRegion = true;
$scope.geoRegionId = '';
$scope.hours = Hours;
console.log('$scope.hours', $scope.hours);
$scope.geoRegion = {
  app_id: $scope.app_id,
  geoRegion_id: '',
  latitude: 37.7879938,
  longitude: -122.40743739,
  name: '',
  address: '',
  radius: 500,
  customer_id: $scope.customer_id
};
_.delay((function() {
  return $scope.setupGoogleMap();
}), 500);
window.test_scope = $scope;
return $scope.updateOrAddGeoRegion = function() {
  var region;
  $scope.loading = true;
  console.log('creating new region with ', $scope);
  region = new GeoRegion($scope.geoRegion);
  console.log('region', region);
  return region.$save((function() {
    return $location.path("/");
  }), function(response) {
    if (response.data.errors) {
      $scope.errors = response.data.errors;
    } else {
      $scope.errors = {
        "Error": ["There was a problem connecting with the server. Please try again later."]
      };
    }
    return $scope.loading = false;
  });
};

} ]); }]);

Do you know how I can format it correctly in a $http POST? 你知道如何在$ http POST中正确格式化吗? In particular, do I need to refactor the success and error into the $http code? 特别是,我是否需要将成功和错误重构为$ http代码? Ie. IE浏览器。 Success redirects to return $location.path("/") or...? 成功重定向返回$ location.path(“/”)或...?

I imagine it's something along these lines, but unsure how to finish it off: 我想这是沿着这些方向的东西,但不确定如何完成它:

$http({
method: 'POST',
url: '',
data: '',
headers: {'Content-Type': 'application/JSON'}

}); });

Though I am reading Angular doc here: https://docs.angularjs.org/api/ng/service/ $http#post I am still a bit confused as to how to directly hit this curl with a $http post? 虽然我在这里阅读Angular doc: https//docs.angularjs.org/api/ng/service/ $ http#post我仍然有点困惑如何用$ http帖子直接点击这个卷曲?

Appreciate any help! 感谢任何帮助!

You're asking for any help, so here's some. 你要求任何帮助,所以这里有一些。 Not sure if I understand what you want to do in case of success, but in order to finish off, you can start with 不确定我是否理解你想要做什么以取得成功,但为了完成,你可以开始

$http({
        url: 'localhost:8080/v1/segments?appkey=c2439fb30a1cad03e9e02bd733ef2ad5',
        method: 'POST',
        data: {'segment' : {'segment_name' : 'ャロットケーキが好き', "segment_type":"static"}},
        headers: {'Content-Type': 'application/json', 'Auth_Token': 'vLC4grMuKbtbamJL3L6x'}
}).success(function (data, status, headers, config) {
            //handle success
            $location.path('/'); //maybe you want to do this  
}).error(function (data, status, headers, config) {
            //handle error
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM