1

単純な odata クエリを実行しようとすると、呼び出しは成功しますが、結果は常に未定義です。URL を説明に挿入し、コピーして貼り付けたところ、問題なく動作しました。オブジェクトが何であるかを確認するために、何十もの異なる方法をテストしましたが、結果は未定義です。私は何が欠けていますか??


更新:以下で説明するように、問題の一部は data.d.results を参照していました。data.d.results[0] を参照すると、実際に「未定義または null 参照のプロパティ '0' を取得できません」というエラー メッセージが表示されました。そのエラーメッセージを検索してもほとんど助けが見つからなかったので、ここに追加したかった.

最終的な答えは、次の組み合わせでした。

  • 1 つの結果のみの data.d
  • システム フィールドの正しい大文字と小文字の区別。「resProd.description」ではなく「resProd.Description」。

元の質問に戻る:

以下は私が使用しているコードです:

    function setOPDefaults() {
        // Create lookup
        var lookupItem = new Array(); 
        lookupItem = Xrm.Page.getAttribute("productid").getValue();

        if (lookupItem != null) 
        {
            var guid = lookupItem[0].id; 
        }

        var crmOrg = window.location.pathname.split('/')[1];
        var serverUrl = window.location.protocol + "//" + window.location.host + (crmOrg == 'userdefined' ? '' : '/' + crmOrg);
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_PREP  = serverUrl + ODATA_ENDPOINT;

        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            // Tried both of the following URLS (obviously not at the same time)
            url: ODATA_PREP + "/ProductSet(guid'" + guid + "')",
            url: "http://crm/<<orgname>>/XRMServices/2011/OrganizationData.svc/ProductSet(guid'67BA90A3-39D8-E211-8D1E-0050569A6113')",
            beforeSend: function (XMLHttpRequest) {   
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                var resProd = data.d.results; 

                alert(resProd.length); // This is undefined
                // Below is where I load the URL into description just for testing.  
                // When I copy and paste this URL into the browser, it pulls up results with correct data
                Xrm.Page.getAttribute("description").setValue(ODATA_PREP + "/ProductSet(guid'" + guid + "')");
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Ajax call failed: " + textStatus + " - " + errorThrown + " || " + XmlHttpRequest.responseText);
            }
        });
    }
4

2 に答える 2