jQuery converting XML tags to uppercase

后端 未结 2 464
刺人心
刺人心 2020-12-21 08:45

I have a AJAX requests which returns a string of XML which I\'d like to inject into the DOM. My function looks like

  $.ajax({
    type: \'POST\',
    url:          


        
相关标签:
2条回答
  • 2020-12-21 09:36

    Try using:

    datatype: 'xml',
    

    instead of 'json', because you're retrieving an XML response. Setting the datatype will result in it being properly parsed as XML.

    0 讨论(0)
  • 2020-12-21 09:44

    From this answer:

    In XML (and XML-based languages such as XHTML), tagName preserves case. In HTML, tagName returns the element name in the canonical uppercase form. The value of tagName is the same as that of nodeName.

    A bit of searching shows that a lot of people experience this problem, especially with selectors and XML (jQuery's find() is case sensitive and selectors used in find() are lowercased).

    Here's a function someone else is using to convert a string into XML that may work for you:

    $.text2xml = function(sXML) { 
        // NOTE: I'd like to use jQuery for this, but jQuery makes all 
        // tags uppercase 
        //return $(xml)[0]; 
        var out; 
        try{ 
            var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser(); 
            dXML.async = false; 
        }catch(e){ throw new Error("XML Parser could not be instantiated"); }; 
        try{ 
            if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false; 
            else out = dXML.parseFromString(sXML, "text/xml"); 
        } 
        catch(e){ throw new Error("Error parsing XML string"); }; 
        return out;
    }
    
    0 讨论(0)
提交回复
热议问题