jquery - 在 XMLHttpRequest 中使用变量名

标签 jquery xml xmlhttprequest

我正在使用 jQuery 并尝试加载一个变量来代替命名的 xml 文件。 我的代码:

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

在上面的代码中我想要:

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

而不是命名文件:

xsl=loadXMLDoc("example.xml");

当我运行代码时,如果我为文件命名它会起作用,但是当我使用变量时(它确实出现在警报中,因此被拉出),它会在上面的行停止代码(将变量作为 xml 文件)

任何帮助将不胜感激!提前谢谢你。

最佳答案

来自评论:

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.

据我所知,像这样的东西应该已经完成​​了你想要的一切:

$(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 教程,因为即使您说的不同,您的代码也一点都没有表明您确实拥有。

关于jquery - 在 XMLHttpRequest 中使用变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2851873/

相关文章:

c# - 如何使用命名空间创建 XElement

javascript - 如何在我的机器上快速设置 "dummy"服务器? (电话间隙可访问)

c# - 异步请求如何用于服务器端验证

jquery - 通过取图像平均颜色设置字体颜色

javascript - 为什么这不是 :after element working?

objective-c - 使用 Objective c 修改现有的 XML 文件

google-chrome - XMLHttpRequest 使用用户名/密码发送 GET HTTP 请求

javascript - 动画改变div的位置

javascript - 如果用户名存在于同一页面上,使用 AJAX 和 PHP 来 "Alert"用户?

xml - golang XML 以 'invalid UTF-8' 错误结束解析