views:

90

answers:

2

Hi All, I am using jQuery and trying to load a variable in place of a named xml file. My Code:

$(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 code above I want to have:

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

instead of a named file:

xsl=loadXMLDoc("example.xml");

When I run through the code, it works if I name the file, but when I use the variable, (which does show up in alerts, so is being pulled), it stops the code at the above line (placing the variable as the xml file)

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

A: 

Ok so I answered my own question: Here is the result for others

1) I nested my function so it could actually reference the variable. 2) I replaced

xml=loadXMLDoc(responseXML2);

with:

xml=responseXML2;

which now worked inside the nested function.

Paul
A: 

From the comments:

I essentially want to post a form to a server, receive the response back in XML, apply an XSLT to the XML and display it in a div on the page.

From what I can see, something like this should already do everything you want:

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

Your code is incredibly riddled with things that jQuery already does for you (like choosing the right XmlHttpRequest object depending on the browser type, and other stuff). You can and should get rid of all of this. And you should begin reading some jQuery tutorials, because even though you say differently, your code does not indicate at all that you really have.

Tomalak
Hi Tomalek.Thank you very much for the code. I am trying this, but when I click the form submit button, nothing happens. I was having this problem with xslt earlier. Perhaps it is something I did wrong in referencing the xslt.js?
Paul
Here is my code for referencing the js: <script type="text/javascript" src="http://www.mydomain.com/xslt.js"></script> <script type="text/javascript" src="http://www.mydomain.com/jquery.xslt.js"></script><script type="text/javascript" src="http://www.mydomain.com/jquery-1.4.2.js"></script> <script type="text/javascript" src="http://www.mydomain.com/jquery.form.js"></script>
Paul
With me just trying the xslt.js and jquery.xslt.js both being in there. Usually it is just one.
Paul
@Paul: *Do* you use `ajaxSubmit()`, like my comment says? P.S.: I have no idea what may be wrong with your referenced script files, and (in all honesty) you should be able to sort that out yourself.
Tomalak
thanks Tomalek, no I wasn't using ajaxSubmit. Let me play around with that and see how it works. Thank you very much!
Paul