简体   繁体   中英

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

I have the following code:

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

The service I'm invoking is returning an XML document with element names in CamelCase. The document begins:

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

The problem I'm having is that when I inspect the returned data object in the debugger (with a breakpoint before the line beginning 'var saml' is executed, I can see that the element names are all lowercase, eg the top-level element is now ns3:requestsecuritytokenresponse.

I can see that the message on the wire is uppercase.

Any ideas how to prevent Ajax from forcing the element names to lowercase?

Thanks.

I think the problem might be that JQuery interprets the data as HTML instead of XML. Since you're not setting the dataType attribute correctly, it will, according to the documentation, try to make an intelligent guess.

Try changing to:

dataType: "xml"
    ^

After that, if you're still seeing the same behavior, it might be just your debugger that's not displaying things correctly. Try displaying the serialized XML with an alert() call and check whether the problem persists.

Note, that in the line where you create the saml variable, you're forcing everything to HTML, so I would expect element names to be lower-cased after that line, but not before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM