c# - "Incorrect Content-Type: "异常抛出 Angular mvc 6 应用程序

标签 c# angularjs asp.net-mvc

我正在我的 Angular 服务上使用 asp.net、mvc6 和 angularjs 开发应用程序。当我向操作方法发出请求时,我没有收到任何传递的数据。 当我检查请求时,我可以看到由以下原因引起的异常:

  • Form '((Microsoft.AspNet.Http.Internal.DefaultHttpRequest)this.Request).Form' threw an exception of type 'System.InvalidOperationException' Microsoft.AspNet.Http.IFormCollection {System.InvalidOperationException}

异常消息说 "Incorrect Content-Type:application/json;charset=UTF-8"

我的 angular.service

return $http({ method: 'POST', url: 'home/createEvent', eventDetails: event })
                .success(function(data, status, headers, config) {
                    return data;
                })
                .catch(function(data, status, headers, config) {
                    console.log(data);
                });

在我的 Controller 上

[HttpPost]
public IActionResult CreateEvent([FromBody]Event eventDetails)
{
    return Json(new {dsd=""},
        new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()});

}

最佳答案

接受的答案对我不起作用。对我来说,解决方案是向 $http 调用添加 header :

        $http({
                url: "/install",
                method: "POST",
                data: data,
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                }
            })
            .success(...);

关于c# - "Incorrect Content-Type: "异常抛出 Angular mvc 6 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711629/

相关文章:

c# - 如何在托管 RDP 连接之外的本地计算机上打开网页?

forms - AngularJS:刷新/重新加载数据并更新 DOM 并使表单保持原始状态

asp.net-mvc - T4MVC 不支持 DisplayTemplates 和 EditorTemplates

c# - 使用 C# 格式化 Twitter 文本 (TweetText)

c# - 将项目部署到 Azure Web 服务时,ASP.NET Core 2.0 Web 应用程序失败并出现 SQLite 错误,但在本地环境中运行良好

c# - 不区分大小写的 GetMethod?

c# - 为什么使用 Regex.IsMatch 检查此字符串会导致 CPU 达到 100%?

c# - 自适应卡 - 以字节为单位提供图像

javascript - 将模型属性方法发送到客户端 Sails Angular

javascript - 无法在 ion-spinner 上执行 ng-click 事件