gpt4 book ai didi

javascript - 在 Angular js 中使用自定义服务发布请求时发生内部服务器错误

转载 作者:行者123 更新时间:2023-11-28 01:49:32 27 4
gpt4 key购买 nike

我是 Angular 的新手,所以我尽力解决这个问题,但经过太多的努力后我不能,这就是为什么要问。实际上,我没有使用 $http 得到响应,我不知道为什么,因为根据我的说法,这似乎是正确的。

这个问题的两个问题 1-发布请求失败 2- CookieStore 如何以 Angular 方式呈现

signUpController.js

app.controller('signUpController', function ($rootScope, $scope, signUpService, $location) {

$scope.addUser = function(){


var user = {
"email": $scope.EmailInfo,
"confirm_email": $scope.ConfirmEmailInfo,
"password": $scope.PasswordInfo,
"confirm_password": $scope.ConfirmPasswordInfo,
"first_name": $scope.FirstnameInfo,
"last_name": $scope.LastNameInfo,
"dob": $scope.dateOfBirth,
"gender": $scope.radioValue
}

signUpService.addNewUser(user).then(function(response) {

if(response.status == 500) {
$rootScope.loggedIn = false;
$location.url('/SignUp');
}else {
$rootScope.loggedIn = true;
$location.url('/Stores');
console.log(data);
}
console.log(response);

});

}
});

signUpService.js

app.factory('signUpService', function($http, $cookieStore) {

var signUpServices = {

addNewUser : function(user) {

var promise = $http({
method:'POST',
url: "/user/register?account_id=" + utils.urlParam('account_id') ,
data: user
}).then(function(response) {

$cookieStore.put("token", response["token"]); // is it write that i used also check please
console.log(response.data);
return response.data;
},
function(reason) {
alert("There was a problem");
});

return promise;
}
};
return signUpServices;
});

错误

POST http://localhost:3000/user/register?account_id=0 500 (Internal Server Error) angular.js:10159
(anonymous function) angular.js:10159
sendReq angular.js:9980
$http.serverRequest angular.js:9757
deferred.promise.then.wrappedCallback angular.js:7303
deferred.promise.then.wrappedCallback angular.js:7303
(anonymous function) angular.js:7340
Scope.$eval angular.js:8685
Scope.$digest angular.js:8548
Scope.$apply angular.js:8771
(anonymous function) angular.js:13970
f.event.dispatch jquery.js:3
h.handle.i

已更新但问题相同

app.factory('signUpService', function($http) {

var signUpServices = {
addNewUser : function(user) {

var promise = $http({method: 'POST', url: "/user/register?account_id=" + utils.urlParam('account_id'),
data: user}).

success(function(data, status, headers, config) {
console.log("response");
alert(data);
}).

error(function(data, status, headers, config) {
alert(status);
});

return promise;
}
};
return signUpServices;
});

如果有人在需要更改的地方纠正此问题,我将非常感激。

最佳答案

您使用的 $http 错误

$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

以下是如何在每个请求上获取 cookie:

        $http.defaults.transformRequest.push(function (data) {
data.csrfToken = $browser.cookies().csrfToken;
return angular.toJson(data);
});

关于javascript - 在 Angular js 中使用自定义服务发布请求时发生内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19876274/

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