javascript - 动态 CRM : JavaScript GET request is not retrieving records using Web. API

标签 javascript asp.net-web-api dynamics-crm dynamics-crm-2016 dynamics-365

我在事件表单上编写了以下 JS 函数来检索契约(Contract)行,但该函数没有执行任何操作。我已经验证了 Fetch Query 并且它返回了结果。所以数据肯定是存在的。我调试了它,看起来“this.readyState == 4”是错误的。

谁能告诉我我的代码有什么问题。我需要添加任何程序集吗?

谢谢

function Test() {

    var customerId = Xrm.Page.getAttribute("parentcustomer").getValue();
    if (customerId == null) {
        return;
    }

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
        "<entity name='contractdetail'>" +
        "<attribute name='contractid' />" +
        "<attribute name='contractdetailid' />" +
        "<filter type='and'>" +
        "<condition attribute='statuscode' operator='in'>" +
        "<value>2</value>" +
        "<value>1</value>" +
        "</condition>" +
        "<condition attribute='customerid' operator='eq' value='" +
        customerId[0].id +
        "' />" +
        "</filter>" +           
        "</entity>" +
        "</fetch>";

    var uri = "/contractdetail?fetchXml=" + encodeURIComponent(fetchXml);
    var clientUrl = Xrm.Page.context.getClientUrl();
    var webAPIPath = "/api/data/v8.1";   
    uri = clientUrl + webAPIPath + uri;

    var request = new XMLHttpRequest();
    request.open("GET", encodeURI(uri), false);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    request.setRequestHeader("OData-MaxVersion", "4.0");
    request.setRequestHeader("OData-Version", "4.0");
    request.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */) {
            request.onreadystatechange = null;

            switch (this.status) {
                case 200: // Success with content returned in response body.
                case 204: // Success with no content returned in response body.
                    var data = JSON.parse(this.response);
                    if (data && data.value) {
                    for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                        alert(data.value[indexContractLine].contractdetailid);
                        //alert(data.value[indexContractLine]['@odata.etag']);
                    }
                }
                    break;
                default: // All other statuses are unexpected so are treated like errors.
                    var error;
                    try {
                        error = JSON.parse(request.response).error;
                    } catch (e) {
                        error = new Error("Unexpected Error");
                    }
                    alert(error);
                    break;
            }

            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data && data.value) {
                    for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                        alert(data.value[indexContractLine].contractdetailid);
                        alert(data.value[indexContractLine]['@odata.etag']);
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        request.send();
    }
}   

最佳答案

onreadystatechange 中将 this 替换为 request

这里有一段直接来自生产环境的示例代码(注意:这是一个 POC,它并不是要复制粘贴到您的代码中)以展示它最终应该是什么样子:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
    if (xhttp.readyState == 4) {
        if (xhttp.status == 200) {
            xhttp.onreadystatechange = null; // avoid memory leaks
            var data = JSON.parse(xhttp.response);
            onsuccess(data);
        }
        else {
            var error = JSON.parse(xhttp.response).error;
            onerror(error);
        }
    }
};

关于javascript - 动态 CRM : JavaScript GET request is not retrieving records using Web. API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43517445/

相关文章:

javascript - gl.texImage2D 适用于 Image,但不适用于 ImageData

javascript - 无法访问 jquery 之外的 var

RegisterServices 中的 NinjectWebCommon 绑定(bind)在 WebApi 中不适用于我

.net - ASP.NET WebAPI 中的模拟和异步

javascript - Dynamics Crm 从外部源调用工作流程

javascript - 从操作脚本调用 javascript 方法后出现长时间运行错误

JavaScript - 将项目分组到数组中

asp.net-web-api - MVC Web API 不适用于 Autofac 集成

dynamics-crm - 数据库还原后 MS CRM 无法添加用户

dynamics-crm - 我什么时候为预验证阶段注册 crm 2011 在线插件?