【问题标题】:HttpPost call from AngularJS throwing 500-Internal Server Error来自 AngularJS 的 HttpPost 调用抛出 500-Internal Server Error
【发布时间】: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


【解决方案1】:

使用

    var config = {headers : {
            "Content-Type": "application/json; charset = utf-8;"
        }
    }; 

并将其传递给

$http.post(apiRoot + "Add", JSON.stringify(userData), config)

因为您将 JSON 数据用于 post 方法并且不提供任何标头。你的邮递员已经包含了这个标题。

【讨论】:

    【解决方案2】:

    感谢所有人为我提供帮助的出色解决方案。最后我自己找到了解决这个问题的方法。有一天它可能会对某人有所帮助。

    1. 在将数据传递给 Angular 服务函数时删除了 JSON.stringify。
    2. 虽然我已将 ng-submit="addUser()" 添加到我的表单中,但我的控制器中没有可用的相应功能。这是我控制器中的服务调用

      userService.addUser($scope.user).then(function (data) { var userData = 数据; });

    改为如下

    $scope.addUser = function () {
    userService.addUser($scope.user).then(function (data) {
                var userData = data;
            });
    }
    

    这些在找到它时看起来很小,但如果不仔细监控,它可能会卡住你一段时间。

    感谢大家的帮助。

    【讨论】:

      【解决方案3】:

      除了对发布数据进行字符串化,尝试将其作为原始数据发送。像这样:

      $http.post(apiRoot + "Add", userData)
           .then(function (response) {
                  def.resolve(response.d`enter code here`ata);
           });
      

      【讨论】:

      • TypeError: (intermediate value)(intermediate value)(intermediate value)(intermediate value) is not a constructor at BlogInfoController.js:38
      【解决方案4】:

      也许您应该尝试将数据作为对象发送

      ...
      $http.post(apiRoot + "Add", userData)
      ...
      

      为什么不直接返回 http 承诺呢?

      addUser: function (userData) {                    
                      return $http.post(apiRoot + "Add", JSON.stringify(userData))
                  }
      

      【讨论】:

      • 在您建议的更改之后。 TypeError: (intermediate value)(intermediate value)(intermediate value)(intermediate value) is not a constructor at BlogInfoController.js:38
      • 您可能有语法错误。你能在 BlogInfoController.js 中显示这些行吗?请
      • 我写的都在帖子里,没有任何改变。
      【解决方案5】:

      实际上一切都是正确的,但 API 返回 500.. 检查 API 是否正确。在 C# 端调试。

      https://www.lifewire.com/500-internal-server-error-explained-2622938

      【讨论】:

      • 原帖末尾有注释,请查收。
      • 试试这样... var api = apiRoot + "Add"; var sendData = JSON.stringify(userData); $http.post(api, sendData);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多