gpt4 book ai didi

javascript - Angularjs 返回错误响应

转载 作者:行者123 更新时间:2023-12-02 15:37:45 25 4
gpt4 key购买 nike

我在注册 Controller 中的响应有问题。由于某种原因,即使用户已保存到数据库,结果也始终是注册失败。为什么会出现这种情况。我对 angularjs - spring 有点陌生,不明白为什么会发生这种情况?

用户.service.js

(function () {
'use strict';

angular
.module('app')
.factory('UserService', UserService);

UserService.$inject = ['$http'];
function UserService($http) {
var service = {};

service.Create = Create;


return service;

function Create(user) {

console.log(user.password)
console.log(user.lastName)
console.log(user.email)
console.log(user.firstName)
console.log(user.customfield)

return $http.post('/api/register', user)
}

})();

并注册.Controller

(function () {
'use strict';

angular
.module('app')
.controller('RegisterController', RegisterController);

RegisterController.$inject = ['UserService', '$location', '$rootScope' ];
function RegisterController(UserService, $location, $rootScope) {
var vm = this;

vm.register = register;

function register() {
vm.dataLoading = true;
UserService.Create(vm.user)
.then(function (response) {
if (response.success) {

console.log("register Succeed")

//FlashService.Success('Registration successful', true);
$location.path('/home');

} else {

console.log("register Failed")
vm.dataLoading = false;
}
});
}
}

})();

注册.html

<div class="container" ng-controller="RegisterController as vm">
<div class="col-md-6 col-md-offset-3">
<h2>Register</h2>
<div ng-show="vm.error" class="alert alert-danger">{{vm.error}}</div>
<form name="form" ng-submit="vm.register()" role="form">


<div class="form-group" ng-class="{ 'has-error': form.lastName.$dirty && form.lastName.$error.required }">
<label for="lastName">Last name</label>
<input type="text" name="lastName" id="lastName" class="form-control" ng-model="vm.user.lastName" required />
<span ng-show="form.lastName.$dirty && form.lastName.$error.required" class="help-block">Last name is required</span>
</div>

<div class="form-group" ng-class="{ 'has-error': form.firstname.$dirty && form.firstname.$error.required }">
<label for="firstName">First name</label>
<input type="text" name="firstName" id="firstName" class="form-control" ng-model="vm.user.firstName" required />
<span ng-show="form.firstName.$dirty && form.firstName.$error.required" class="help-block">First name is required</span>
</div>


<div class="form-group" ng-class="{ 'has-error': form.customfield.$dirty && form.customfield.$error.required }">
<label for="customfield">Custom field</label>
<input type="text" name="customfield" id="customfield" class="form-control" ng-model="vm.user.customfield" required />
<span ng-show="form.customfield.$dirty && form.customfield.$error.required" class="help-block">customfield is required</span>
</div>

<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
<label for="email">Username</label>
<input type="text" name="email" id="email" class="form-control" ng-model="vm.user.email" required />
<span ng-show="form.email.$dirty && form.email.$error.required" class="help-block">email is required</span>
</div>


<div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="vm.user.password" required />
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Register</button>
<a href="#/login" class="btn btn-link">Cancel</a>
</div>
</form>
</div>
</div>

最佳答案

根据the documentation :

The response object has these properties:

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

您正在测试 response.success,这不是文档所说存在的属性。请查看状态

A response status code between 200 and 299 is considered a success status and will result in the success callback being called.

关于javascript - Angularjs 返回错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com