【问题标题】:Response body not parsed when API returns http 400API 返回 http 400 时未解析响应正文
【发布时间】:2019-07-30 13:31:21
【问题描述】:

Powerapps Model Driven / Dynamics Client API 发出请求时,如果您在任何查询字符串中出现错误,则会返回一个空白错误对象。即使响应正文包含错误对象,它也不会被解析。

按照上面为retrieveMultipleRecords 链接的文档中的示例,我有一个实体,其中包含一个名为new_OfferedBy 的用户的查找字段。为了将其过滤到特定用户,您需要过滤/systemuserid。如果任何属性的名称错误(区分大小写),您会收到 400 响应。下面的示例将返回 400 响应。

// this query looks for the systemuser property when it should look for systemuserid
// this.currentUser returns the guid of the current user

var query = "?$filter=new_OfferedBy/systemuser eq (" + this.currentUser() +")";

Xrm.WebApi
    .retrieveMultipleRecords("new_lastaskswap", query)
    .then(
        function success(result) {
            console.log("Result Success:");
            console.log(result);
            // perform additional operations on retrieved records
        },
        function (error) {
            console.log("Error from .then():");
            console.log(error);
            // handle error conditions
        }
    )

它将以下信息输出到控制台,这根本没有用。

{errorCode: 2147951872, message: "", code: 2147951872, innerror: undefined}

但是,如果您在 Chrome 开发工具中查看请求的实际响应,您会发现响应正文中填充了 JSON,以一种有用的方式描述错误:

为什么响应体在返回 400 时没有被 API 解析,这不是预期的行为吗?

【问题讨论】:

  • 我不能专门针对 Dynamics API 发言,但是在 PowerApps 中创建自定义连接器(用于 Canvas 应用程序)时,您必须定义可能的响应。您是否可以选择在 Dynamics 中将 400 响应定义为连接的一部分?
  • 你会认为这就是错误函数的用途,但它不会解析响应

标签: javascript dynamics-crm-online dynamics-crm-webapi powerapps-modeldriven


【解决方案1】:

我相信您的代码中有 2 个错误,

  1. 您的查询可能是错误的,字段new_OfferedBy 是查找 对于 Systemuser 但你不需要 /systemuser 你可以直接给 用户指南。
  2. Xrm.WebApi.online.retrieveMultipleRecords 而不是Xrm.WebApi .retrieveMultipleRecords

    var query = "?$filter=new_OfferedBy eq (" + this.currentUser().replace(/[{}]/g, "") +")";

        Xrm.WebApi.online.retrieveMultipleRecords("new_lastaskswap", query)
            .then(
                function success(result) {
                    console.log("Result Success:");
                    console.log(result);
                    // perform additional operations on retrieved records
                },
                function (error) {
                    console.log("Error from .then():");
                    console.log(error);
                    // handle error conditions
                }
            )
    

这是我的帐户实体代码之一

Xrm.WebApi.online.retrieveMultipleRecords("account", "?$select=accountid,accountnumber&$filter=_createdby_value eq 1234567897").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
            var accountid = results.entities[i]["accountid"];
            var accountnumber = results.entities[i]["accountnumber"];
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);

【讨论】:

  • 另外,你实际上错了 1。它应该是new_OfferedBy/systemuserid,最后有 id,如果你不这样做,响应正文包含一个错误说 "A binary operator with incompatible types was detected. Found operand types 'Microsoft.Dynamics.CRM.systemuser' and 'Edm.Guid' 但它没有t 返回错误函数
  • 我没有看到任何故意抛出错误 400 的问题。W.r.t new_OfferedBy/systemuserid 我刚刚在没有 systemuserid 的情况下对 Dynamics crm 发起了一个查询,它是否返回了正确的值 _createdby_value eq 940A9E4A-623A-E911 -A831-000D3A385E93"。因此不需要systemuserid。无论如何,当我基本上触发webapi调用并且存在拼写错误或任何其他错误时,它确实会给出错误详细信息,我什至看到你得到响应为Xrm.Utility.alertDialog(error.message) ;
  • 您使用的是_createdby_value ,这与字段名称不同。我的问题是我在请求响应的正文中收到错误,但它没有被 api 解析到错误函数中,这实际上是问题的标题..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 2015-04-03
  • 2014-12-27
  • 2016-06-20
  • 2022-06-13
  • 2020-06-16
相关资源
最近更新 更多