【问题标题】:Http Post Angularjs causing internal server error via Web APIHttp Post Angularjs通过Web API导致内部服务器错误
【发布时间】:2019-10-17 13:13:58
【问题描述】:

我正在通过 web API 从 angularjs 进行简单的 http post insert 操作。但我不知道为什么我不断收到内部服务器错误。 http GET 功能工作正常。但是对于 http 帖子,数据已通过 WebAPI 成功插入数据库,但未收到来自服务的响应。收到内部服务器错误 500。

WebAPI

 // POST: api/Website/InsertEmployee
[ResponseType(typeof(Employee))]
[HttpPost]
public IHttpActionResult InsertEmployee(Employee employee)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    objCSADBEntities.Employee.Add(employee);
    objCSADBEntities.SaveChanges();

    return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
}

Angularjs 服务

(function () {

    var app = angular.module('myApp');

    app.factory('websiteService', ['$http', function ($http) {
        var factory = [];

        factory.insertEmployee = function (webAPIHostName,employee) {
            var request = $http({
                method: 'POST',
                url: webAPIHostName + '/Website/InsertEmployee',
                data: employee
            })
            return request;
        }

        return factory;
    }
   ]);
})();

Angularjs 控制器(在那里调用服务我收到内部服务器错误 500)

var employee = {
    EmployeeID: 1,
    Name: 'Test',
    Phone: '111-111-1111'
};

var insertEmployee = websiteService.insertEmployee($scope.webAPIHostName , employee);
insertEmployee.then(function (response) {
    alert("Submitted Employee Successfully.");
    //success response here
}, function (error) {
    // error handling here
});

【问题讨论】:

    标签: angularjs asp.net-web-api http-post


    【解决方案1】:

    您能否使用 fiddler 或 postman 验证您的 API 调用是否返回了您期望的结果?一旦您可以验证 WebAPI 正在返回有效的 HttpStatus(200 到

    内容类型:application/json

    如果您的响应不是您所期望的,请尝试从

    更改您的 API 返回
    return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
    

    return Request.CreateResponse(HttpStatusCode.OK, employee);
    

    如果您可以验证您的 POST 是否按预期工作,请检查您的 angularjs。

    对于 Angularjs,你的 http 调用可以更新为

    angular.module('myApp').factory('websiteService', ['$http', function ($http) {            
        var insertEmployee;
    
        insertEmployee = function (webAPIHostName, employee) {            
            return $http.post(webAPIHostName + '/Website/InsertEmployee', JSON.stringify(employee));
        }
    
        return {
            insertEmployee: insertEmployee
        };
    }]);
    
    
    
    // Angular Controller
    
    var employee = {
        EmployeeID: 1,
        Name: 'Test',
        Phone: '111-111-1111'
    };
    
    websiteService.insertEmployee($scope.webAPIHostName , employee)
        .then(function (response) {
            alert("Submitted Employee Successfully.");
            //success response here
          }, function (error) {
            // error handling here
        });
    

    【讨论】:

    • 这在我的本地工作,但是当我部署到服务器时,我得到了这个......我需要做任何 iis 服务器配置
    • 是否可以在服务器上配置一些东西来运行 webapi post
    • 您是否使用过 Postman 或 Fiddler 来验证您是否从实时服务器返回了 200?你能发布你从 500 错误中得到的确切响应错误吗?
    • 使用$http 服务,无需对JavaScript 对象数据进行字符串化。 AngularJS 框架自动完成。
    猜你喜欢
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2014-05-26
    • 2013-09-08
    • 2023-02-20
    • 2010-11-23
    • 2022-01-04
    • 2016-07-29
    相关资源
    最近更新 更多