jquery ajax调用强制响应中的XML元素名称为小写

jquery ajax call is forcing XML element names in the response to lowercase

本文关键字:元素 XML 调用 ajax 响应 jquery      更新时间:2023-09-26

我有以下代码:

  var frm = $('#login');   
  frm.submit(function (ev) {
    var postData = $(this).serializeArray();
    $.ajax({
      type: frm.attr('method'),
      url: frm.attr('action'),
      datatype: "xml",
      data: postData,
      success: function (data, textStatus, jqXHR) {
        var saml = $("saml2'':Assertion", (new XMLSerializer()).serializeToString(data));
        var $assertion = $('<div/>').text(saml[0].outerHTML);
        $('<pre/>').appendTo('#body').text(vkbeautify.xml($assertion.text()));
      },
      error: function (jqXHR, textStatus, errorThrown) {
        alert('login failed - '.concat(jqXHR.responseText));
      }
    });

我正在调用的服务返回一个XML文档,该文档具有CamelCase中的元素名称。文件开始:

<ns3:RequestSecurityTokenResponse xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns10="http://www.w3.org/2000/09/xmldsig#" xmlns:ns2="http://www.w3.org/2005/08/addressing" xmlns:ns3="http://docs.oasis-open.org/ws-sx/ws-trust/200512/" xmlns:ns4="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns5="http://docs.oasis-open.org/ws-sx/ws-trust/200802" xmlns:ns6="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:ns7="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns8="http://schemas.xmlsoap.org/ws/2005/02/sc" xmlns:ns9="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
<ns3:RequestedSecurityToken>

我遇到的问题是,当我在调试器中检查返回的数据对象时(在执行以"var saml"开头的行之前有一个断点),我可以看到元素名称都是小写的,例如,顶级元素现在是ns3:requestsecuritytokenresponse。

我可以看到电线上的信息是大写的。

有什么想法可以防止Ajax强制元素名称小写吗?

谢谢。

我认为问题可能是JQuery将数据解释为HTML而不是XML。由于您没有正确设置dataType属性,根据文档,它将尝试进行智能猜测。

尝试更改为:

dataType: "xml"
    ^

之后,如果您仍然看到相同的行为,可能只是调试器没有正确显示内容。尝试使用alert()调用显示序列化的XML,并检查问题是否仍然存在。

请注意,在创建saml变量的那一行中,您将所有内容都强制为HTML,因此我希望元素名称在那一行之后小写,而不是在之前。