簡體   English   中英

Angular:將curl轉換為Angular $ http POST請求

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

我有這個卷曲線:

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"}}'

現在curl直接命中了API。 我希望能夠在保存和重定向回到路由'返回區域后執行$ 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;
  });
};

}]);

你知道如何在$ http POST中正確格式化嗎? 特別是,我是否需要將成功和錯誤重構為$ http代碼? IE瀏覽器。 成功重定向返回$ location.path(“/”)或...?

我想這是沿着這些方向的東西,但不確定如何完成它:

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

});

雖然我在這里閱讀Angular doc: https//docs.angularjs.org/api/ng/service/ $ http#post我仍然有點困惑如何用$ http帖子直接點擊這個卷曲?

感謝任何幫助!

你要求任何幫助,所以這里有一些。 不確定我是否理解你想要做什么以取得成功,但為了完成,你可以開始

$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