简体   繁体   中英

Send a HTTP request in AngularJS

I have a uri link that if I call it, it will send me a notification on my email address which is http://localhost:8081/subscribe . If I use it in my browser it returns a JSON string : {"subscriptionArn":"pending confirmation"} and sends an email to email account and it works fine.

Now, I'd like to call this link and use it in my page web which made in AngularJS , I want to, when I click on the create button, call the link in the controller and send the notification on my email account.

I made a small code but it doesn't work , here is my code :

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

By looking at your code. You should redirect after the call has been complete by passing a callback function in the subscribe method

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

Probably, you should use this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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