【问题标题】:Error in HTTP Post from Angular into Web API .NET Core从 Angular 到 Web API .NET Core 的 HTTP Post 出错
【发布时间】:2020-05-12 06:49:42
【问题描述】:

我正在尝试将 JSON 数据从 Angular 9 发布到 webapi。

这是我的 POST 代码

  const headers=new HttpHeaders().set("Content-Type",'application/json');
  let url:string=this.isServer?"":"https://localhost:44331/api";
  console.info(JSON.stringify(data));

  return this.client.post<any>(url+'/Crrequests',JSON.stringify(data),
  {headers,responseType:"json",withCredentials:true});

当我执行它时会触发一个错误提示

“出现一个或多个验证错误”

在开发者控制台中。

但我用 POSTMAN 检查了同一组 JSON。它工作正常。

所以没有想法有什么问题。

【问题讨论】:

    标签: angular asp.net-core http-post


    【解决方案1】:

    只需发布数据,无需字符串化

    return (this.client.post<any>(url + "/Crrequests", data,{ headers, responseType: "json", withCredentials: true }));
    

    仅将此 JSON.stringify(data) 替换为 data

    【讨论】:

      【解决方案2】:

      也许这种方法可能会对您有所帮助,这就是我向后端发出 Http 请求的方式。您也可以订阅它并将其登录到控制台中,这样您就可以看到后端的答案。希望对你有帮助

              var headers = new HttpHeaders({"Content-Type": "application/json"});
              var url= "https://localhost:44331/api";
              return this.client.post<any>(url + "/Crrequests", 
                       {
                          //  Json object you want to post, like
                          // Email: data.email
                          // Password: data.password
                       },
                       {
                           headers
                       }); 
      

      【讨论】:

        猜你喜欢
        • 2018-01-24
        • 2017-03-29
        • 2017-08-05
        • 1970-01-01
        • 2020-10-20
        • 2018-10-04
        • 1970-01-01
        • 2020-03-04
        • 2021-09-14
        相关资源
        最近更新 更多