从xml响应中获取一些数据来自ajax jquery调用

时间:2022-10-09 11:18:36

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.

我有一个post类型的ajax调用,成功地获得了下面的xml响应,我想从下面显示的响应中获取“This is response”。

            $.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.

请告诉我答案。

2 个解决方案

#1


2  

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();

#2


0  

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

看看XMLHttpRequest。如果想使用原始文本,可以使用XmlHttpRequest.responseText

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

如果希望使用XML格式,请使用jQuery parseXML解析返回的XML。例如:

...
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)

或者使用普通Javascript(从这里开始)

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

#1


2  

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();

#2


0  

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

看看XMLHttpRequest。如果想使用原始文本,可以使用XmlHttpRequest.responseText

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

如果希望使用XML格式,请使用jQuery parseXML解析返回的XML。例如:

...
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)

或者使用普通Javascript(从这里开始)

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