我试图使用SOAP请求获取Priority Code
实体的Task
选项设置值。
if (typeof (SDK) == "undefined")
{ SDK = { __namespace: true }; }
SDK.SOAPSamples = {
_getServerUrl: function () {
var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
var serverUrl = "";
if (typeof GetGlobalContext == "function") {
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == "object") {
serverUrl = Xrm.Page.context.getServerUrl();
}
else
{ throw new Error("Unable to access the server URL"); }
}
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + OrgServicePath;
},
assignRequest: function (successCallback, errorCallback) {
if (successCallback != null)
this._parameterCheck(successCallback, "Function", "The SDK.SOAPSamples.assignRequest method successCallback parameter must be a function.");
this._parameterCheck(errorCallback, "Function", "The SDK.SOAPSamples.assignRequest method errorCallback parameter must be a function.");
var request = "POST https://tamaldomain.api.crm5.dynamics.com/XRMServices/2011/Organization.svc/web";
request += "Content-Type: text/xml; charset=utf-8";
request += "SOAPAction: http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute";
request += "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>";
request += "<s:Body>";
request += "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>";
request += "<request i:type='a:RetrieveAttributeRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>";
request += "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>MetadataId</b:key>";
request += "<b:value i:type='c:guid' xmlns:c='http://schemas.microsoft.com/2003/10/Serialization/'>00000000-0000-0000-0000-000000000000</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>RetrieveAsIfPublished</b:key>";
request += "<b:value i:type='c:boolean' xmlns:c='http://www.w3.org/2001/XMLSchema'>false</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>EntityLogicalName</b:key>";
request += "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>task</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>LogicalName</b:key>";
request += "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>prioritycode</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil='true' />";
request += "<a:RequestName>RetrieveAttribute</a:RequestName>";
request += "</request>";
request += "</Execute>";
request += "</s:Body>";
request += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", SDK.SOAPSamples._getServerUrl(), true)
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.onreadystatechange = function () { SDK.SOAPSamples.assignResponse(req, successCallback, errorCallback); };
req.send(request);
},
assignResponse: function (req, successCallback, errorCallback) {
debugger;
if (req.readyState == 4) {
if (req.status == 200) {
if (successCallback != null)
{ successCallback(); }
}
else {
errorCallback(SDK.SOAPSamples._getError(req.responseXML));
}
}
}
问题是req.readyState
的值为1,表示“已建立服务器连接”。但我不知道究竟是什么意思。
如何将req.readyState
的值更改为4,以便我可以从此请求中获得响应?
非常感谢。
答案 0 :(得分:0)
onreadystatechange事件被触发五次(0-4),readyState中的每次更改都会触发一次。请参阅here有关此状态的更多信息。
答案 1 :(得分:0)
我发现javascript中的编程有些情况很麻烦。因此,我不是单独使用javascript而是创建一个“Generic Handler”(.ashx)来编写我的解决方案,并使用XmlHttpRequest对象从javascript执行此处理程序。
答案 2 :(得分:0)
尝试将req移动到SDK.SOAPSamples范围,然后在assignResponse中使用它,如下所示:
SDK.SOAPSamples = {
req: null,
_getServerUrl: function () { … },
assignRequest: function (successCallback, errorCallback) {
//...
var req = SDK.SOAPSamples.req = new XMLHttpRequest();
//...
},
assignResponse: function (successCallback, errorCallback) { //remove req param
var req = SDK.SOAPSamples.req;
if (req.readyState == 4) {
if (req.status == 200) {
if (successCallback != null) { successCallback(); }}
else { errorCallback(SDK.SOAPSamples._getError(req.responseXML));}}
}
}