Javascript 来自 AngularJS 的 HttpPost 调用抛出 500-内部服务器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43343143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpPost call from AngularJS throwing 500-Internal Server Error
提问by Dash
I'm developing a application where I need to add an user to database thus require POST operation. However everytime I run the application I get 500-Internal Server Error for the POST api call.
我正在开发一个应用程序,我需要将用户添加到数据库中,因此需要 POST 操作。但是,每次我运行该应用程序时,我都会收到 500-内部服务器错误的 POST api 调用。
The following is my code snippet.
以下是我的代码片段。
angular.module('app')
.constant('userApiRoot', 'http://localhost:82/api/User/');
Service.js
服务.js
app.factory('userService', [
'userApiRoot', '$http', '$q', function (apiRoot, $http, $q) {
'use strict';
return {
apiRoot: apiRoot,
addUser: function (userData) {
var def = $q.defer();
$http.post(apiRoot + "Add", JSON.stringify(userData))
.then(function (response) {
def.resolve(response.d`enter code here`ata);
}, def.reject);
return def.promise;
}
};
}
]);
Controller.js
控制器.js
app.controller('BlogInfoController', ['userService', '$scope', BlogInfoController]);
function BlogInfoController(userService, $scope) {
'use strict';
$scope.user = {};
userService.addUser($scope.user).then(function (data) {
var userData = data;
});
}
HTML
HTML
<div class="comments-form">
<h3 class="title-normal">Leave A Comment</h3>
<form role="form" ng-submit="addUser()">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="name" id="name" placeholder="Your Name" ng-model="user.Name" type="text" required>
</div>
<div class="form-group">
<input class="form-control" name="email" id="email" placeholder="Your Mail" ng-model="user.Email" type="email" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Your Website" type="text" ng-model="user.Website" required>
</div>
</div>
</div>
<div class="text-right">
<button class="btn btn-primary" type="submit">Post Comment</button>
</div>
</form>
</div>
The error I'm getting is in the attached screenshot.
I've tried finding a solution but nothing helps. I get the same error across all browsers.
我试过寻找解决方案,但没有任何帮助。我在所有浏览器中都遇到相同的错误。
NOTE - The same api call works perfectly when called using external client like postman.
注意 - 使用外部客户端(如邮递员)调用时,相同的 api 调用可以完美运行。
TIA.
TIA。
回答by Dash
Thanks to all for their fine solutions to help me out. Finally I found solution to this problem by myself. It might will help somebody someday.
感谢所有人为我提供了很好的解决方案。最后我自己找到了解决这个问题的方法。有一天它可能会帮助某人。
- Removed the JSON.stringify while passing data to angular service function.
Although I've added ng-submit="addUser()" to my form there was no respective function available in my controller. It was the service call in my controller
userService.addUser($scope.user).then(function (data) { var userData = data; });
- 在将数据传递给角度服务函数时删除了 JSON.stringify。
尽管我已将 ng-submit="addUser()" 添加到我的表单中,但我的控制器中没有相应的功能可用。这是我的控制器中的服务调用
userService.addUser($scope.user).then(function (data) { var userData = data; });
Changed it to the following
将其更改为以下
$scope.addUser = function () {
userService.addUser($scope.user).then(function (data) {
var userData = data;
});
}
These looks small misses upon finding it but it can stuck you for a while if not monitored closely.
这些在发现时看起来很小,但如果不密切监视,它可能会让您卡住一段时间。
Thanks everybody for your help.
谢谢大家的帮助。
回答by Muhammed Neswine
Instad of stringifying the post data, try to send it as raw data. Like this:
在字符串化发布数据的同时,尝试将其作为原始数据发送。像这样:
$http.post(apiRoot + "Add", userData)
.then(function (response) {
def.resolve(response.d`enter code here`ata);
});
回答by Der Flo
Maybe you should try so send the data as an object
也许您应该尝试将数据作为对象发送
...
$http.post(apiRoot + "Add", userData)
...
and why dont you return the http promise directly?
为什么不直接返回 http 承诺?
addUser: function (userData) {
return $http.post(apiRoot + "Add", JSON.stringify(userData))
}
回答by sgajera
Use
用
var config = {headers : {
"Content-Type": "application/json; charset = utf-8;"
}
};
and pass it to
并将其传递给
$http.post(apiRoot + "Add", JSON.stringify(userData), config)
$http.post(apiRoot + "Add", JSON.stringify(userData), config)
because you are using JSON data for post method and doesn't provide any header. Your postman have already included this header.
因为您将 JSON 数据用于 post 方法并且不提供任何标头。您的邮递员已包含此标题。
回答by Manikandan Velayutham
Actually Everything is correct but API is return 500.. Check API is correct. Debug in C# side.
实际上一切都正确,但 API 返回 500 .. 检查 API 是否正确。在 C# 端调试。
https://www.lifewire.com/500-internal-server-error-explained-2622938
https://www.lifewire.com/500-internal-server-error-explained-2622938

