如何解决一个 Javascript 中的 2 个 XMLHTTPRequests (Dynamics CRM)
我目前正在尝试在动态 CRM Javascript 中运行 2 个单独的 XMLHTTPRequest,以从 2 个不同的实体检索数据并根据检索的内容运行代码..
出于安全原因,我已尽力尝试编辑一些名称,但前提是相同的。 我的主要问题是第一次运行 XMLHTTPRequest(RA 横幅)工作正常,但是第二次运行(状态横幅)返回的 Readystate 是 2 并且正在停止。
function FormBanners(formContext) {
//Clear the existing banners
formContext.ui.clearFormNotification("Notif1");
formContext.ui.clearFormNotification("Notif2");
//Get the customer/rep
var customer = formContext.getAttribute("customerid").getValue();
var rep = formContext.getAttribute("representative").getValue();
var contact;
//use the rep if there is one else use the customer
if (rep != null) {
contact = rep;
}
else if (customer!= null) {
contact = customer;
}
//Get the account
var account = formContext.getAttribute("accountfield").getValue();
//As there is a requirement for 2 XMLHTTPRequests we have to queue them
var requestURLs = new Array();
//There will always be a customers or rep on the form
requestURLs.push(Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts?$select=new_RA,new_SC,new_VC,new_PR&$filter=contactid eq " + contact[0].id + "",true);
//there may not be an account
if (account) {
requestURLs.push(Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?$select=_new_statusLookup_value&$filter=accountid eq " + account[0].id + "",true);
}
var current = 0;
function getURL(url) {
var req = new XMLHttpRequest();
req.open("GET",requestURLs[current]);
req.setRequestHeader("OData-MaxVersion","4.0");
req.setRequestHeader("OData-Version","4.0");
req.setRequestHeader("Accept","application/json");
req.setRequestHeader("Content-Type","application/json; charset=utf-8");
req.setRequestHeader("Prefer","odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(req.response);
// Creation of the RA Banner
if (current == 0) {
var RA = result.value[0]["new_RA@OData.Community.Display.V1.FormattedValue"];
var SC = result.value[0]["new_SC@OData.Community.Display.V1.FormattedValue"];
var VC = result.value[0]["new_VC@OData.Community.Display.V1.FormattedValue"];
var PR = result.value[0]["new_PR@OData.Community.Display.V1.FormattedValue"];
var Notif = "";
//Only create a notification if any of the above contain "Yes"
if (RA == "Yes" || SC == "Yes" || VC == "Yes" || PR == "Yes") { Notif = "The Customer/Rep has:" }
if (RA == "Yes") { Notif = Notif + " RA,"; }
if (SC == "Yes") { Notif = Notif + " SC,"}
if (VC == "Yes") { Notif = Notif + " VC,"}
if (PR == "Yes") { Notif = Notif + " PR."}
if (Notif != "") {
formContext.ui.setFormNotification(Notif,"INFO","Notif1");
}
}
//Creation of the Status Banner
else if (current == 1) {
status = results.value[i]["_new_statusLookup_value"];
if (status) {
if (status[0].name != "Open")
var Notif = "The status for the organisation on this case is " + status[0].name + ".";
formContext.ui.setFormNotification(Notif,"Notif2");
}
}
++current;
if (current < requestURLs.length) {
getURL(requestURLs[current]);
}
} else {
Xrm.Utility.alertDialog(req.statusText);
}
}
}; req.send();
}
getURL(requestURLs[current]);
}
有人可以帮我吗?
解决方法
所以在我的例子中我犯了很多错误。根据 Arun 的建议,我将函数分离出来,这使得调试变得容易得多。它并没有在 XMLHTTP 请求上失败,因为它运行良好,但是调试时 firefox 崩溃了。
所以我的问题是我在第二部分:
- 将 i 的值用于结果(不再定义 i)
- 获取“_new_statusLookup_value”只会提供查找的 guid 我想要“_new_statusLookup_value@OData.Community.Display.V1.FormattedValue”
- 不要忽视这样一个事实,因为我复制并粘贴了此代码的多次迭代,因此我也使用了“results”而不是“result”。
很多小错误......但它发生了!感谢您的帮助,只是为了显示......错别字是我们的垮台!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。