javascript - How to get response from ajax success call - Stack Overflow

I want to get response of ajax success call in variable 'response' from where I called functi

I want to get response of ajax success call in variable 'response' from where I called function .

Call of function:

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

Function:

 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", "");
                },
                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);
                }
            });
}

Please suggest me answer.

I want to get response of ajax success call in variable 'response' from where I called function .

Call of function:

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

Function:

 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./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);
                }
            });
}

Please suggest me answer.

Share Improve this question asked Sep 6, 2016 at 6:14 chhaya_patelchhaya_patel 1711 gold badge2 silver badges16 bronze badges 1
  • use global variable instead of return statment – Basheer AL-MOMANI Commented Sep 6, 2016 at 8:02
Add a ment  | 

2 Answers 2

Reset to default 3

This is not how JS asynchronous functions are meant to be used. You should be using callbacks, unless you're using es6 in which you should use promises. But if for some reason you absolutely had to get it to work, you could do something like this (note, i did not test this).

 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./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;
 }

Here's how to use a callback with the function

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./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);
         }
     });
 }

called as such

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

Your problem is that you are using a sync function when AJAX is async,

you can use a 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./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);
    }
});

and then use it like that

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744317086a4568236.html

相关推荐

  • javascript - How to get response from ajax success call - Stack Overflow

    I want to get response of ajax success call in variable 'response' from where I called functi

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信