角度:将卷曲角$ HTTP POST请求 [英] Angular: Convert curl to Angular $http POST request

查看:247
本文介绍了角度:将卷曲角$ HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的卷曲行:

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

现在的卷曲直接击中API。我希望能够做一个$ HTTP POST保存和重定向恢复航线后返回区域$保存((函数(){。

Now that curl directly hits the API. I'd like to be able to do a $http POST after the save and redirection back to route 'return region.$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 code? IE浏览器。成功重定向到返回$ location.path(/),还是...?

Do you know how I can format it correctly in a $http POST? In particular, do I need to refactor the success and error into the $http code? Ie. Success redirects to return $location.path("/") or...?

我想这是这些方针的东西,但不知道该如何来完成它:

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

});

虽然我在这里读书角DOC: https://docs.angularjs.org/api / NG /服务/ $ HTTP#后,我还是有点困惑,如何直接打这个卷曲以$ HTTP POST?

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?

鸭preciate任何帮助!

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
});

这篇关于角度:将卷曲角$ HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆