简体   繁体   English

在AngularJS中发送HTTP请求

[英]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 . 我有一个uri链接,如果我叫它,它将在我的电子邮件地址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. 如果我在浏览器中使用它,它将返回一个JSON字符串: {"subscriptionArn":"pending confirmation"}并将电子邮件发送到电子邮件帐户,并且可以正常工作。

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

I made a small code but it doesn't work , here is my code : 我编写了一个小代码,但是它不起作用,这是我的代码:

service.js: 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: 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 调用完成后,您应该通过在subscribe方法中传递一个回调函数来重定向

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

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

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