【发布时间】:2017-09-06 16:13:57
【问题描述】:
我正在开发一个应用程序,我需要将用户添加到数据库,因此需要 POST 操作。 但是,每次我运行应用程序时,我都会收到 500-Internal Server Error for POST api 调用。
以下是我的代码sn-p。
angular.module('app')
.constant('userApiRoot', 'http://localhost:82/api/User/');
Service.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
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
<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>
我已尝试找到解决方案,但没有任何帮助。我在所有浏览器中都遇到相同的错误。
注意 - 使用 postman 等外部客户端调用时,相同的 api 调用可以完美运行。
TIA。
【问题讨论】:
-
如果有500响应,就是连接不好,最好放chrome的网络控制台
-
不要把post参数转成字符串
-
删除 stringify 后出现此错误:TypeError: (intermediate value)(intermediate value)(intermediate value)(intermediate value) is not a constructor at BlogInfoController.js:38
-
显示你的 C# 代码
-
邮递员可以工作。但是来自带有 angularjs 的 c# 请求是正确的。
标签: javascript html angularjs asp.net-mvc asp.net-web-api