【问题标题】:Postman POST working but AngularJS post throwing 404邮递员 POST 工作,但 AngularJS 投掷 404
【发布时间】:2018-07-17 01:22:38
【问题描述】:

我只是想通过 AngularJS $http 将一些基本的表单数据传递给 web-api。

这是调用以将数据发送到 API 的函数:

    $http({
        url: "/Portal/GenerateTimeSheets",
        method: "POST",
        headers: {
            'Content-Type': 'application/json'
        },
        data: angular.toJson($scope.placementForm),
    }).then(function (response) {

    }), function(response) {

    };

注意:如果我断点并将 $scope.placementForm 数据复制并粘贴到邮递员中,它完全可以正常工作,但是通过浏览器会引发错误。

这是我的 api:

   [HttpPost]
    public void GenerateTimeSheets([FromBody]PlacementModel placement)
    {
        Console.WriteLine("STUB");
    }

和放置模型:

    [JsonProperty(PropertyName = "candidateName")]
    public string CandidateName { get; set; }

    [JsonProperty(PropertyName = "clientName")]
    public string ClientName { get; set; }

    [JsonProperty(PropertyName = "jobTitle")]
    public string JobTitle { get; set; }

    [JsonProperty(PropertyName = "placementStartDate")]
    public string StartDate { get; set; }

    [JsonProperty(PropertyName = "placementEndDate")]
    public string EndDate { get; set; }

    [JsonProperty(PropertyName = "frequency")]
    public string TimeSheetFrequency { get; set; }

【问题讨论】:

  • 404表示你请求的路径错误
  • 路径是 100% 正确的,这就是为什么让我感到困惑 @Sajeetharan
  • 那很奇怪,我可以看看网络标签的截图
  • 如果可能,请发布邮递员中显示的请求数据和 devtools 网络选项卡中显示的请求数据。
  • 开发工具中的请求是什么?很可能路径是错误的

标签: angularjs asp.net-web-api


【解决方案1】:

404 通常表示请求的 url 错误,你在 url 中遗漏了一些东西。使用后端验证您的网址。希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2015-10-29
    • 2014-04-27
    • 2017-03-10
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    相关资源
    最近更新 更多