开发者

Using a variable name in XMLHttpRequest

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();
  xsl开发者_运维知识库tProcessor.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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜