Ich benutze Knoten, Express, Mungo für Backend und eckige js für Front-End. Ich sende eine Postanfrage mit einem Formular.Wie kann ich die Antwort einer POST-Anfrage mit Angular JS bekommen?
<div class="alert alert-success" ng-show="success">
{{success}}
</div>
<div class="alert alert-danger" ng-show="error">
{{error}}
</div>
<form id="newCategory" ng-submit="submit()">
<div class="row">
<div class="col-md-4">
{{mySubmit}}
<div class="form-group">
<input id="name" name="name" type="text"
placeholder="Name" class="form-control input-md"
ng-model="catName">
</div><!-- ./form-group -->
</div><!-- ./col -->
<div class="form-group">
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</div><!-- ./form-group -->
</div><!-- ./row -->
</form>
Sein Controller:
fetchModule.controller('CatsNewCtrl', ['$scope', '$rootScope', 'Request',
function($scope, $rootScope, Request) {
// Root Scope
$rootScope.heading = 'Categories';
$rootScope.heading2 = 'new';
$rootScope.newLink = undefined;
$scope.catName = "";
$scope.submit = function() {
Request.post('/category', { name : $scope.catName })
.then(function(data) {
$scope.success = data.msg;
})
.catch(function(status) {
$scope.error = "Error: " + status;
});
return false;
};
}]);
Ergebnis ist nichts, ich weiß nicht, wohin ich falsch gehe! Dieser Dienst funktioniert einwandfrei. Ich konnte das Ergebnis zwar per Konsole protokollieren, aber ich kann es nicht für den Bereich festlegen.
(function() {
let fetchModule = angular.module('fetchModule', []);
/**
* A Service for Requests.
*/
fetchModule.service('Request', ['$http', function($http) {
/**
* Get's content of a specific URL.
* @param {String} url - URL to GET.
* @return {Promise} Promise resolves with contents or rejects with an error.
*/
this.get = function(url) {
// Promise
return new Promise((resolve, reject) => {
$http.get(url)
.success(result => {
resolve(result);
})
.error((err, status) => {
reject(err);
});
});
};
/**
* Get's content of a specific URL.
* @param {String} url - URL to GET.
* @param {String} data - Data to post.
* @return {Promise} Promise resolves with contents or rejects with an error.
*/
this.post = function(url, data) {
// Promise
return new Promise((resolve, reject) => {
$http.post(url, data)
.success((data, status, headers, config) => {
resolve(data);
})
.error((data, status, headers, config) => {
resolve(status);
});
});
};
Bitte helfen, ich bin nur ein Winkelanlasser.
Der Rückruf der Erfolgsmethode hat die Daten nicht, was Sie wollen? – user93
Es ist! keine Probleme bis dahin, aber ich kann das Ergebnis in der Warnung nicht sehen –
können Sie console.log (data.message) und überprüfen Sie statt data.msg? – Ved