Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从xml中获取一些数据响应来自ajax jquery调用_Javascript_Jquery_Ajax_Xml - Fatal编程技术网

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

Javascript 从xml中获取一些数据响应来自ajax jquery调用,javascript,jquery,ajax,xml,Javascript,Jquery,Ajax,Xml,我有一个类型为post的ajax调用,成功后得到如下所示的xml响应。我想从下面显示的响应中获取“thisaresponse” $.ajax({ type: "POST", contentType: "text/xml; charset=utf-8", datatype: "xml", url: serverUrl + "/XRMServices

我有一个类型为post的ajax调用,成功后得到如下所示的xml响应。我想从下面显示的响应中获取“thisaresponse”

            $.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>
new\u pasmcreateprojectresponse此响应

请告诉我答案。

您是否使用成功方法中的数据进行了检查

$.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();
看一看。如果要使用原始文本,可以使用
XmlHttpRequest.responseText

如果要使用XML格式,请使用解析返回的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(从)


您好,Haresh,我检查了控制台,但它显示了与我得到的相同的响应。@chhaya_patel,我得到了解决方案,用
$(数据)检查。查找('b\\\:value')。text()如果您喜欢并更正,请标记为“正确”,并向上投票;)您好,我使用$(data.find('b\\\:value').text();,没有得到任何值;。它给了我“.try with
$(XmlHttpRequest.responseText)。find('b\\\:value').text()”
Hi Firice Nguyen,我使用$.parseXml在responsexml中得到空值。您是否包含了
jQuery
?您使用的是哪个版本的
jQuery
?是的。我正在使用或您可以使用纯Javascript,请参阅我的更新答案HI和您的旧答案现在我可以在var responsexml中获得值,更改为XmlHttpRequest.responseText,而不是XmlHttpRequest.responsexml。但我无法在它提供给对象的控制台中获得确切的b:值
...
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);
  }
},
...