0

运行我的 JavaScript 时,单击左图所示表单上的按钮会触发,我在代码中的某个点处获得多个日志。JavaScript 向外部 API 发出 XHR 请求。 在此处输入图像描述

当我从控制台输出单击任何指向我的文件的链接时,我就会被带到那里。在我导航到我的 JavaScript 文件之前,但似乎不能再导航了。 在此处输入图像描述

JavaScript 源代码

function HandleSyncTask(commandProperties, primaryControl) {

  console.log("HandleSyncTask running...");
  openSyncTaskConfirmDialog(commandProperties, primaryControl);
}

function openSyncTaskConfirmDialog(commandProperties, primaryControl) {

  console.log("Opening confirm dialog...");
  var confirmStrings = {
      cancelButtonLabel: "No",
      confirmButtonLabel: "Yes",
      title: "Preparing Tasks Sync",
      subtitle: "Are you sure you want to Sync Tasks?"
  };
  var confirmOptions = { height: 200, width: 450 };

  Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    success => {
        if (success.confirmed) {
          console.log("openSyncTaskConfirmDialog confirmed.");
          getOrgTasks(commandProperties, primaryControl);
        }
        else {
          console.log("openSyncTaskConfirmDialog closed.");
        }
    },
    error => {
        console.log(error.message);
  });
}

function openSyncTaskCompleteDialog() {

  var alertStrings = { confirmButtonLabel: "Okay", text: "Sync task complete" };
  var alertOptions = { height: 120, width: 260 };
  Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
      function success(result) {
          console.log("openSyncTaskCompleteDialog dialog closed");
      },
      function (error) { concole.log(error.message); }
  );
}

function getOrgTasks(commandProperties, primaryControl) {

    console.log("In getOrgTasks");
    var stsToken = "";
    var tokenReq = new XMLHttpRequest();
    var tokenReqParams = "grant_type=password&scope=OrgApi&username={REMOVED FOR STACKOVERFLOW}&password={REMOVED FOR STACKOVERFLOW}";
    tokenReq.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
        if (this.status == 200) {
          console.log("SUCCESS sts/connect/token");
          var tokenObject = JSON.parse(this.responseText);
          stsToken = tokenObject.access_token.toString();
        }
      }
    });
    tokenReq.open("POST", "https://orgdev.azurewebsites.net/sts/connect/token", true);
    taskReq.setRequestHeader("Accept", "application/json");
    tokenReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    tokenReq.setRequestHeader("Authorization", "Basic {REMOVED FOR STACKOVERFLOW}");
    tokenReq.setRequestHeader("Cache-Control", "no-cache");
    console.log("About to SEND sts/connect/token");
    tokenReq.send(tokenReqParams);

    var challengeTasks = [];
    var orgConfigurationEntityReference = primaryControl.getGrid().getRows().get(0).data.entity.getEntityReference();
    var orgConfigurationEntityType = orgConfigurationEntityReference["entityType"].toString();
    var orgConfigurationGuid = orgConfigurationEntityReference["id"].toString();
    Xrm.WebApi.retrieveRecord(orgConfigurationEntityType, orgConfigurationGuid, "?$expand=org_org_orgconfiguration_org_orgchallenge_orgconfigurationid").then(
        function success(result) {
            console.log("SUCCESS retrieveRecord");
            var taskReq = new XMLHttpRequest();
            taskReq.withCredentials = true;
            taskReq.addEventListener("readystatechange", function () {
              if (this.readyState === 4) {
                if (this.status == 200) {
                  challengeTasks = JSON.parse(this.responseText);
                 }
              }
            });
            taskReq.open("GET", `https://orgdev.azurewebsites.net/api/${result.org_org_orgconfiguration_org_orgchallenge_orgconfigurationid[0]["org_orgid"]}/tasks`, false);
            taskReq.setRequestHeader("Accept", "application/json");
            taskReq.setRequestHeader("Authorization", "Bearer " + stsToken);
            taskReq.setRequestHeader("Content-Type", "application/json");
            taskReq.setRequestHeader("Cache-Control", "no-cache");
            taskReq.send();

            var orgTrigger = { };
            var orgEntityName = "";

            Xrm.Utility.showProgressIndicator("Syncing Tasks...");

            for (var i = 0; i < challengeTasks.length; i++) {

              orgEntityName = challengeTasks[i].primaryobjecttypecode[0].toUpperCase() + challengeTasks[i].primaryobjecttypecode.slice(1);
              orgTrigger =
              {
                "org_name": challengeTasks[i].name + " " + orgEntityName,
                "org_entityname": orgEntityName,
                "org_messagename": challengeTasks[i].name,
                // org_org_orgtask_org_orgtrigger
                // navigation property (1:N), DataModel/orgCrmSdkTypes, generated class using the CrmSvcUtil.exe
                "org_org_orgtrigger_org_orgtask_orgtriggerid":
                [
                  {
                    "org_name": challengeTasks[i].Title,
                    "org_orgtaskidreference": challengeTasks[i].TaskId,
                    "org_enabled": true,
                    "org_pointvalue": challengeTasks[i].EligiblePoints
                  }
                ]
              };
              Xrm.WebApi.createRecord("org_orgtrigger", orgTrigger).then(

                function success(result) {
                  console.log("Created task and attached trigger to it, trigger is " + orgTrigger);
                },
                function (error) {
                    console.log("ERROR: Xrm.WebApi.createRecord " + error.message.toString());
                }
              );
            }

            setTimeout(function() {
              console.log("Creating an illusion of loading something");
              Xrm.Utility.closeProgressIndicator();
              openSyncTaskCompleteDialog();
            }, 5000);
        },
        function (error) { console.log(error.message); }
    );
}
4

1 回答 1

0

删除所有接受 json 的 setRequestHeader 解决了我的问题。还在新实现中实现了 Promises 和链调用,以强制执行我希望数据到达我的解决方案的顺序。

所以删除每一行。

{request var name goes here}.setRequestHeader("Accept", "application/json");
于 2018-05-25T18:31:44.590 回答