【问题标题】:How to pass an Object Model (that has an Array of Objects Models in it) to API for insert as Params如何将对象模型(其中包含对象模型数组)传递给 API 以作为参数插入
【发布时间】:2020-06-03 05:54:45
【问题描述】:

我是 Angular 的新手,试图为我的对象模型制作参数,每当我对我的对象进行字符串化时,它都会生成一个 API 不接受的大字符串。我该怎么办..?

这是我要转换为参数的对象。

{
    "qaevaluationid": 1,
    "agentid": 1,
    "callerid": "1234",
    "calledon": "02/13/2020 10:38:14 AM",
    "duration": "304",
    "overallfeedback": "adasdasd",
    "isfatal": false,
    "fatalcallreasonid": "3",
    "evaluationtypeid": "1",
    "callratings": [{
        "callratingid": 1,
        "createdby": 1,
        "createdbyname": "john",
        "createdon": null,
        "evaluationfactorid": 8,
        "is_deleted": "F",
        "modifiedby": -1,
        "modifiedbyname": "",
        "modifiedon": null,
        "qaevaluationid": 1,
        "rating": "7"
    }, {
        "callratingid": 2,
        "createdby": 1,
        "createdbyname": "john",
        "createdon": null,
        "evaluationfactorid": 9,
        "is_deleted": "F",
        "modifiedby": -1,
        "modifiedbyname": "",
        "modifiedon": null,
        "qaevaluationid": 1,
        "rating": "6"
    }, {
        "callratingid": 3,
        "createdby": 1,
        "createdbyname": "john",
        "createdon": null,
        "evaluationfactorid": 10,
        "is_deleted": "F",
        "modifiedby": -1,
        "modifiedbyname": "",
        "modifiedon": null,
        "qaevaluationid": 1,
        "rating": "8"
    }],
    "createdby": 1,
    "createdbyname": "John",
    "createdon": null,
    "modifiedby": -1,
    "modifiedbyname": null,
    "modifiedon": null,
    "is_deleted": "F"
}

在 TypeScript 中我是这样做的

this.httpOptions.params = new HttpParams();

this.httpOptions.params = this.httpOptions.params.set('qaEval', JSON.stringify(qaevaluation)); 

return this._httpClient.post<APIResponse<QAEvaluation>>(this.myAppUrl + 'QAEvaluation/insert', { qaEval: qaevaluation }, this.httpOptions)
      .pipe(retry(1), catchError(this.errorHandler));

收到此错误

"Http 失败响应为 https://localhost:44304/QAEvaluation/insert?qaEval=%7B%22qaevaluationid%22:-1,%22agentid%22:1,%22callerid%22:%221234%22,%22calledon%22:%2202/13/2020%2010:38:14%20AM%22,%22duration%22:%22304%22,%22overallfeedback%22:%22adasdasd%22,%22isfatal%22:false ……。这种情况一直持续下去:404 OK"

【问题讨论】:

  • 在您的 localhost:44304/... 上运行的服务器是否可以获取此字符串并返回结果?在我看来,没有。并且最好发送对象的唯一 ID 而不是这样的完整长字符串
  • 不,我没有在本地主机上运行任何其他可以通过大字符串返回结果的服务器。以及如何发送对象的唯一 ID,然后在 Api End 将其转换为结果?

标签: c# angular typescript api http-options-method


【解决方案1】:

您在正确的路径上,但您将所有 json 对象作为查询参数添加到请求的 URL 上。在 POST 请求中,您希望将数据放入 HTTP 正文中。

我认为如果您省略第二行,您就可以继续使用。还请查看 HttpClient 文档以获取示例和正确的语法。

【讨论】:

    【解决方案2】:

    您很可能需要将您的 URL 代理到您的 api。 Angular 使用特定端口在您的开发环境中托管,在您的情况下为 44304

    检查:https://angular.io/guide/build

    您可以使用 webpack 开发服务器中的代理支持将某些 URL 转移到后端服务器,方法是将文件传递给 --proxy-config 构建选项。例如,要将所有对http://localhost:4200/api 的调用转移到在http://localhost:3000/api 上运行的服务器,请执行以下步骤。

    在项目的 src/ 文件夹中创建一个文件 proxy.conf.json。

    将以下内容添加到新的代理文件中:

    {
      "/api": {
        "target": "http://localhost:3000",
        "secure": false
      }
    }
    

    在 CLI 配置文件 angular.json 中,将 proxyConfig 选项添加到服务目标:

    "architect": {
      "serve": {
        "builder": "@angular-devkit/build-angular:dev-server",
        "options": {
          "browserTarget": "your-application-name:build",
          "proxyConfig": "src/proxy.conf.json"
        },
    

    按服务运行

    【讨论】:

      【解决方案3】:

      更新了我的控制器以接受带有动态对象的 FormBody,并从 typescript 中删除了 http 参数,例如......并且它有效。

      return this._http.post<APIResponse<QAEvaluation>>(this.myAppUrl + 'QAEvaluation/insert', { qaEval: qaevaluation }, { headers: new HttpHeaders({ 'Content-Type': 'application/json; charset=utf-8' }) })
                .pipe(retry(1), catchError(this.errorHandler));
      

      还使用序列化来解析我的结果对象

          public ActionResult Insert([FromBody]dynamic qaEval)
                  {            
      
                          var objModel1 = Newtonsoft.Json.JsonConvert.DeserializeObject(qaEval.ToString());
      
                          var objModel = Newtonsoft.Json.JsonConvert.SerializeObject(objModel1.qaEval);
      
          QAEvaluationModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.QAEvaluationModel>(objModel);
      
      qaEvaluationRepository.Add(model);
      
      return Ok(new Models.APIResponse
                          {
                              HasError = false,
                              Message = "Success",
                              Content = null
                          });
          }
      

      【讨论】:

        猜你喜欢
        • 2019-03-11
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 2017-12-29
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多