未触发 XMLHTTPRequest onReadyStateChange
XMLHTTPRequest onReadyStateChange Not Triggered
我正在实施 XMLHTTPRequest
,但无法触发 onreadystatechange
功能。
请问如何解决?谢谢。
function DisableDuplicateModuleCreation(context) {
debugger;
var saveEvent = context.getEventArgs();
var user_userid= Xrm.Page.getAttribute("user_userid").getValue();
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/modules?$select=user_userid&$filter=user_userid eq '" + user_userid + "' and statecode eq eq", false);
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=\"*\",odata.maxpagesize=1");
req.onreadystatechange = function () { //last stop of this code and jumps to the end without going into function
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
alert("results.entities.length: " + results.entities.length);
if (results.entities.length > 0) {
alert("Module failed to create due to an existing module for User ID " + user_userid + ". Please update the existing module.");
saveEvent.preventDefault();
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
} //jumps to end of code after => req.onreadystatechange = function () from above
您已经创建了var/objectreq
但是您还没有发送这个对象,
你错过了 req.send();
这是一个示例 XMLHttpRequest
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts?$select=address2_county&$filter=accountid eq 1234567899", false);
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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var address2_county = results.value[i]["address2_county"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
如果我可以更进一步并建议,请尝试使用 Xrm.Webapi
来执行 webapi 请求。 Microsoft 强烈推荐使用 Xrm.Webapi
注意:Xrm.Webapi
是异步的,您可能希望使用 promise 使其像 Sync/or 等待您的请求完成一样工作
Xrm.WebApi.online.retrieveMultipleRecords("account", "?$select=address2_county&$filter=accountid eq 1234567899").then(
function success(results) {
for (var i = 0; i < results.entities.length; i++) {
var address2_county = results.entities[i]["address2_county"];
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
我正在实施 XMLHTTPRequest
,但无法触发 onreadystatechange
功能。
请问如何解决?谢谢。
function DisableDuplicateModuleCreation(context) {
debugger;
var saveEvent = context.getEventArgs();
var user_userid= Xrm.Page.getAttribute("user_userid").getValue();
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/modules?$select=user_userid&$filter=user_userid eq '" + user_userid + "' and statecode eq eq", false);
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=\"*\",odata.maxpagesize=1");
req.onreadystatechange = function () { //last stop of this code and jumps to the end without going into function
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
alert("results.entities.length: " + results.entities.length);
if (results.entities.length > 0) {
alert("Module failed to create due to an existing module for User ID " + user_userid + ". Please update the existing module.");
saveEvent.preventDefault();
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
} //jumps to end of code after => req.onreadystatechange = function () from above
您已经创建了var/objectreq
但是您还没有发送这个对象,
你错过了 req.send();
这是一个示例 XMLHttpRequest
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts?$select=address2_county&$filter=accountid eq 1234567899", false);
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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var address2_county = results.value[i]["address2_county"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
如果我可以更进一步并建议,请尝试使用 Xrm.Webapi
来执行 webapi 请求。 Microsoft 强烈推荐使用 Xrm.Webapi
注意:Xrm.Webapi
是异步的,您可能希望使用 promise 使其像 Sync/or 等待您的请求完成一样工作
Xrm.WebApi.online.retrieveMultipleRecords("account", "?$select=address2_county&$filter=accountid eq 1234567899").then(
function success(results) {
for (var i = 0; i < results.entities.length; i++) {
var address2_county = results.entities[i]["address2_county"];
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);