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

时间:2016-09-05 10:43:27

标签: javascript jquery ajax xml

我有一个类型为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);
                }
            });

响应:

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

请给我答案。

2 个答案:

答案 0 :(得分:2)

您是否已使用成功方法检查数据?

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

您可以在控制台中看到对象..您可以轻松地从对象中检索数据...

之类的东西
$(data).find('b\\:value').text();

答案 1 :(得分:0)

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

如果要使用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);
  }
},
...

或使用普通Javascript(来自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);
  }
},
...