1

我试图使用 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.readyState1,表示“服务器连接已建立”。但我不知道这到底是什么意思。

如何将 的值更改req.readyState为 4 以便从该请求中获得响应?

非常感谢。

4

3 回答 3

0

onreadystatechange 事件被触发五次(0-4),readyState 的每一次变化触发一次。有关此状态的更多信息,请参见此处。

于 2013-03-19T13:36:01.093 回答
0

我发现在某些情况下使用 javascript 编程很麻烦。因此,我创建了一个“通用处理程序”(.ashx)来编写我的解决方案并使用 XmlHttpRequest 对象从 javascript 执行此处理程序,而不是单独使用 javascript。

于 2013-03-20T15:32:54.573 回答
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));}}
    }
}
于 2013-11-22T12:45:36.750 回答