chhaya_patel
chhaya_patel

Reputation: 171

Fetch some data from xml response comes from ajax jquery call

I have ajax call with type post and in success I get xml response as per below .And I want to fetch "This is Response" from the response displayed below.

            $.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) {
                    debugger;
                    if (XmlHttpRequest.status === 200) {
                        var response = XmlHttpRequest.responseXML;
                        alert(XmlHttpRequest.responseText);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });

Response:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services"><ExecuteResult xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ResponseName>new_PASMcreateProject</a:ResponseName><a:Results xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"><a:KeyValuePairOfstringanyType><b:key>Response</b:key><b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">This is response</b:value></a:KeyValuePairOfstringanyType></a:Results></ExecuteResult></ExecuteResponse></s:Body></s:Envelope>

Please suggest me the answer.

Upvotes: 1

Views: 1554

Answers (2)

Haresh Vidja
Haresh Vidja

Reputation: 8496

Have you checked with data in success method?

$.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) {
                   alert($(XmlHttpRequest.responseText).find('b\\:value').text()); // check this in console.log
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });

You can see in console with object.. you can retrieve data from object easily... something like

$(data).find('b\\:value').text();

Upvotes: 2

An Nguyen
An Nguyen

Reputation: 1478

Take a look at XMLHttpRequest. If you want to use raw text, you can use XmlHttpRequest.responseText

If you want to use XML format, use jQuery parseXML to parse the returned XML. For example:

...
success: function (data, textStatus, XmlHttpRequest) {
  if (XmlHttpRequest.status === 200) {
    var responseXML = $.parseXML(XmlHttpRequest.responseXML),
      $responseXML = $(responseXML),
      $responseValue = $responseXML.find('b\\:value');
    console.log(responseXML);
  }
},
...

Or using plain Javascript (from here)

...
success: function (data, textStatus, XmlHttpRequest) {
  if (XmlHttpRequest.status === 200) {
    parser = new DOMParser();
    responseXML = parser.parseFromString(XmlHttpRequest.responseXML, "text/xml");
    var response = responseXML.getElementsByTagName('b\\:value')[0].childNodes[0].nodeValue;              
    console.log(response);
  }
},
...

Upvotes: 0

Related Questions