繁体   English   中英

在AngularJS中发送HTTP请求

[英]Send a HTTP request in AngularJS

我有一个uri链接,如果我叫它,它将在我的电子邮件地址http://localhost:8081/subscribe上向我发送通知。 如果我在浏览器中使用它,它将返回一个JSON字符串: {"subscriptionArn":"pending confirmation"}并将电子邮件发送到电子邮件帐户,并且可以正常工作。

现在,我想调用此链接并在AngularJS中创建的网页中使用它,我想当我单击“创建”按钮时,调用控制器中的链接并通过我的电子邮件帐户发送通知。

我编写了一个小代码,但是它不起作用,这是我的代码:

service.js:

var services = angular.module('ngdemoApp.services', ['ngResource']);
services.factory('Subscription', function ($resource) {
    return $resource('http://localhost:8081/subscribe', {}, {
        subscribe: { method: 'GET' }
    });
});

controller.js:

var app = angular.module('ngdemoApp.controllers', []);
app.controller('CustomerListCtrl', ['$scope','GetCustomersFactory', '$location','Subscription',
           function ($scope, GetCustomersFactory, $location,Subscription) {

                // click-button to edit the customer
                $scope.editCustomer = function (customerId) {
                  $location.path('/customer-edit/' + customerId);   
                }

                // click-button to delete the customer
                $scope.deleteCustomer = function () {
                  //DeleteCustomerFactory.remove({ id: customerId });
                  $location.path('/customers');
                };

               // click-button to create a customer
               //here exactely where i'd to like call the link
               $scope.createNewCustomer = function () {
                   Subscription.subscribe();
                   $location.path('/customer-add');
               };

                   $scope.customers = GetCustomersFactory.query();
           }]);

通过查看您的代码。 调用完成后,您应该通过在subscribe方法中传递一个回调函数来重定向

 $scope.createNewCustomer = function () {
       Subscription.subscribe(function(){
          $location.path('/customer-add');
       });
 };

可能,您应该使用以下代码:

Subscription
  .subscribe().$promise
  .then(function () {
    $location.path('/customer-add');
});

暂无
暂无

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

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