如何从ajax成功通话中获得响应

时间:2016-09-06 06:14:31

标签: javascript jquery ajax

我希望在变量'响应'中得到ajax成功调用的响应。从我称之为功能。

调用功能:

 var response = ExecuteAction(Id,Entityname,Processname);

功能:

 function ExecuteAction(entityId, entityName, requestName) {
                $.ajax({
                type: "POST",
                contentType: "text/xml; charset=utf-8",
                datatype: "xml",
                url: serverUrl + "/XRMServices/2011/Organization.svc/web",
                data: requestXML,
                async:false,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
                    XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                    debugger;
                    if (XmlHttpRequest.status === 200) {
                        var response = $(XmlHttpRequest.responseText).find('b\\:value').text();
                        return response;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
}

请建议我回答。

2 个答案:

答案 0 :(得分:2)

这不是JS异步函数的使用方式。你应该使用回调,除非你使用es6,你应该使用promises。但如果由于某种原因你绝对不得不让它工作,你可以做这样的事情(注意,我没有测试这个)。

 function ExecuteAction(entityId, entityName, requestName) {
     var response;
     $.ajax({
         type: "POST",
         contentType: "text/xml; charset=utf-8",
         datatype: "xml",
         url: serverUrl + "/XRMServices/2011/Organization.svc/web",
         data: requestXML,
         async: false,
         beforeSend: function(XMLHttpRequest) {
             XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
             XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
         },
         success: function(data, textStatus, XmlHttpRequest) {
             debugger;
             if (XmlHttpRequest.status === 200) {
                 response = $(XmlHttpRequest.responseText).find('b\\:value').text();
             }
         },
         error: function(XMLHttpRequest, textStatus, errorThrown) {
             alert(errorThrown);
         }
     });
     while (!response);
     return response;
 }

以下是如何使用函数

进行回调
function ExecuteAction(entityId, entityName, requestName, callback) {
     var response;
     $.ajax({
         type: "POST",
         contentType: "text/xml; charset=utf-8",
         datatype: "xml",
         url: serverUrl + "/XRMServices/2011/Organization.svc/web",
         data: requestXML,
         async: false,
         beforeSend: function(XMLHttpRequest) {
             XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
             XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
         },
         success: function(data, textStatus, XmlHttpRequest) {
             debugger;
             if (XmlHttpRequest.status === 200) {
                 response = $(XmlHttpRequest.responseText).find('b\\:value').text();
                 callback(null, response);
             }
         },
         error: function(XMLHttpRequest, textStatus, error) {
             alert(error);
             callback(error);
         }
     });
 }

这样称呼

var response = ExecuteAction(Id,Entityname,Processname, function(err, result) {
    if (err) console.log('whoops, error', err);
    console.log('I will print upon completion', result);
});

答案 1 :(得分:1)

您的问题是,当AJAX是异步时,您正在使用同步功能

你可以使用Promise -

return new Promise( (resolve, reject) => {
    $.ajax({
    type: "POST",
    contentType: "text/xml; charset=utf-8",
    datatype: "xml",
    url: serverUrl + "/XRMServices/2011/Organization.svc/web",
    data: requestXML,
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
    XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    },
    success: function (data, textStatus, XmlHttpRequest) {
        if (XmlHttpRequest.status === 200) {
            var response = $(XmlHttpRequest.responseText).find('b\\:value').text();
            resolve(response);
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
            reject(errorThrown);
    }
});

然后像那样使用它

var responsePromise = ExecuteAction(Id,Entityname,Processname);

responsePromise.then( (response) => {
    console.log(response)
},
(error) => {
    console.log(error)
});