简体   繁体   中英

how to call/return ajax success object out of a function

I'm trying to retrieve attribute values, I'm setting ajax get to a variable. Console.log returns the ajax object but I'm unable to return the object in success.

I have tried: ajaxObj.d ajaxObj.responseJSON.d ajaxObj..new_SubType.Value

I would like to retrieve specifically var obj = JSON.parse(XmlHttpRequest.responseText).d; out of getAccountDetails()

function getAccountDetails() {
var accountObject = Xrm.Page.getAttribute("parentcustomerid").getValue();
if ((accountObject != null)) {
    var accountObjectId = accountObject[0].id;
    var clientUrl = Xrm.Page.context.getClientUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; //Xrm OData end-point
    var odataSetName = "AccountSet";
    odataSetName = encodeURIComponent(odataSetName);
    accountObjectId = encodeURIComponent(accountObjectId);
    var odataSelect = clientUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + accountObjectId + "')";
    var ajaxObj =
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            var obj = JSON.parse(XmlHttpRequest.responseText).d;
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
    });
console.log(ajaxObj);
}
}

I'm following dynamics sdk RESTjquerycontacteditor script from sdk 2015. Currently working on crm online

Replace the line

var resultContact = data.d;

with this:

var resultContact = JSON.parse(XmlHttpRequest.responseText).d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM