[英]Change element style in EJS after POST action in Node - Express
[英]node express and angular post action
介紹
我有一個有角度的帖子,發布到我的節點后方后端,該后端將數據發送到API,如果發布成功,我希望將用戶發送到另一個有角度的頁面,如果不正確,則希望顯示錯誤消息。
我在想,如果成功將變量發布到我的角度,則可以在我的范圍內使用ng隱藏/顯示或加載新頁面(如果存在變量)。
但是,也許有更好的方法可以做到這一點?
我的角貼
FirstModule.controller('LoginController', function ($scope, $http) {
$scope.formData = {};
$scope.LoginForm = function () {
var data = {
LoginEmail: $scope.formData.LoginEmail
};
$http({
url: 'http://localhost:8080/back-end/test',
method: "POST",
data: data,
headers: {'Content-Type': 'application/json'}
}).success(function (data) {
$scope.formData = data; // assign $scope.persons here as promise is resolved here
}).error(function (data, status) {
$scope.formData = status;
});
}
});
我的節點將帖子發布到A API
這篇文章使用了從我的角度發送的電子郵件並將其發送到第三方API,我知道希望采取行動
{ var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/x-www-form-urlencoded' }; // Configure the request var api = result[1].data_api; var login_email = result[0].data_login_email; var options = { url: 'https://pi.pardot.com/api/prospect/version/4/do/read', method: 'POST', headers: headers, form: { 'email': login_email, 'user_key': userkey, 'api_key': api }, json: true // Automatically stringifies the body to JSON }; // Start the request rp(options) .then(function (parsedBody) { console.error(login_email, "Is a user, login pass!"); // $scope.FormLogin = true; }) .catch(function (err) { console.error("fail no such user"); }); console.error("Third done"); }
更好的方法是從API發送響應代碼,以告知Angular請求是否成功。
如果您使用Express,則可以執行以下操作:
if (everythingOk) {
res.status(200).send(successData)
} else {
res.status(400).send()
}
並在您的角度要求中:
rp(options)
.then(function (parsedBody) {
if(parsedBody.statusCode == 200) {
console.error(login_email, "Is a user, login pass!");
} else {
console.error("fail no such user");
// $scope.FormLogin = true;
}
})
.catch(function (err) {
console.error("unknown error");
});
console.error("Third done");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.