在AngularJS中发送HTTP请求
我有一个uri链接,如果我调用它,它会在我的电子邮件地址上向我发送一个通知,地址是在AngularJS中发送HTTP请求,angularjs,Angularjs,我有一个uri链接,如果我调用它,它会在我的电子邮件地址上向我发送一个通知,地址是http://localhost:8081/subscribe。如果我在浏览器中使用它,它将返回一个JSON字符串:{“subscriptionArn”:“待定确认”},并向电子邮件帐户发送一封电子邮件,效果良好 现在,我想调用这个链接并在我的网页中使用它,这个网页是用AngularJS制作的,我想,当我点击创建按钮时,调用控制器中的链接并在我的电子邮件帐户上发送通知 我编写了一个小代码,但不起作用,以下是我的代码
http://localhost:8081/subscribe
。如果我在浏览器中使用它,它将返回一个JSON字符串:{“subscriptionArn”:“待定确认”}
,并向电子邮件帐户发送一封电子邮件,效果良好
现在,我想调用这个链接并在我的网页中使用它,这个网页是用AngularJS制作的,我想,当我点击创建按钮时,调用控制器中的链接并在我的电子邮件帐户上发送通知
我编写了一个小代码,但不起作用,以下是我的代码:
service.js:
var services = angular.module('ngdemoApp.services', ['ngResource']);
services.factory('Subscription', function ($resource) {
return $resource('http://localhost:8081/subscribe', {}, {
subscribe: { method: 'GET' }
});
});
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();
}]);
controller.js:
var services = angular.module('ngdemoApp.services', ['ngResource']);
services.factory('Subscription', function ($resource) {
return $resource('http://localhost:8081/subscribe', {}, {
subscribe: { method: 'GET' }
});
});
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');
});
是否出现任何错误?没有错误,但我的电子邮件地址上没有收到任何邮件,但当我直接使用浏览器中的链接时,它在firefox或chrome中运行finePress F12并查看网络选项卡。这意味着我将修改service.js中的服务方法?@rochdibuslama否,service.js中的服务已编码为接受回调。@RochdiBuslama您需要在控制器本身中执行此操作。.在其中传递函数将调用subscribe方法的resolve