Using a variable name in XMLHttpRequest

I am using jQuery and trying to load a variable instead of a named XML file. My code is:

$(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#theForm').ajaxForm(function(responseXML2) { 

            var myxml = responseXML2;
            alert(responseXML2);
            displayResult();


           }); 
}); 
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  alert("loading xmlhttprequest");
  xhttp=new XMLHttpRequest();
  }
else
  {
  alert("loading activeX");
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
alert("bottom load");
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}

function displayResult()
{
alert("setting vars");

alert("displayResult called");

//xml=loadXMLDoc(responseXML2);  //tried this and the line below, among others
xml=responseXML2;
alert("xmlDocLoaded");
xsl=loadXMLDoc("xslt-test.xsl");
alert("XSLloaded");
// code for IE
if (window.ActiveXObject)
  {
  alert("IE");
  ex=xml.transformNode(xsl);
  document.getElementById("ieiresponse").innerHTML=ex;
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  alert("notIE");
  xsltProcessor=new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml,document);
  document.getElementById("ieiresponse").appendChild(resultDocument);
  }
}

In the above code I want:

//xml=loadXMLDoc(responseXML2);  //tried this and the line below, among others
xml=responseXML2;

instead of the file name:

xsl=loadXMLDoc("example.xml");

When I run the code, it works if I name the file, but when I use this variable (which appears in the warnings, therefore it is pulled), it stops the code in the specified line (placing the variable as an xml file)

Any help would be greatly appreciated! Thank you in advance.

+3
source share
2 answers

From the comments:

I really want to submit the form to the server, get the response back in XML, apply XSLT in XML and display it in a div on the page.

, , - , :

$(document).ready(function() {
  // prepare sending the AJAX form (use ajaxSubmit() to actually send it)
  $('#theForm').ajaxForm({
    dataType: 'xml', 
    success:  function(responseText, statusText, xhr, $form) {
      // jQuery xslt plugin required for this:
      $.xslt({
        xml: xhr.responseXML,
        xslUrl: "xslt-test.xsl",
        target: "#ieiresponse"
      });
    },
    error: function(xhr, textStatus, errorThrown) {
      alert("Oops, there was an error: " + textStatus);
    }
  });
});

, jQuery (, XmlHttpRequest ..). . jQuery, , -, , .

+2

, :

1) , . 2)

xml=loadXMLDoc(responseXML2);

:

xml=responseXML2;

.

0

Source: https://habr.com/ru/post/1745858/


All Articles