2010-05-17 115 views
3

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

$(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文件中)

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

+0

你有jQuery并做类似'if(window.XMLHttpRequest)'的事情吗?为什么?! – Tomalak 2010-05-17 19:01:56

+0

我使用jQuery来检索表单发布的XML响应。如果有更好的方法(我是XML新手),我非常开放! – Paul 2010-05-17 19:09:21

+0

我正在查看更多的代码,它看起来像responseXML2没有被拉入新的功能。我试图把现有的displayresult()代码底部在这里下: $( '#theForm')给ajaxForm(函数(responseXML2){ VAR myxml = responseXML2; 警报(responseXML2); 配售代码在这里 没有运气,现在它甚至没有调用警报! – Paul 2010-05-17 19:10:47

回答

2

从评论:

我基本上是想表单POST到服务器, 接收响应回XML,应用XSLT 到XML,并在页面上的一个div显示它。

从我所看到的,这样的事情应该已经做你想要的一切:

$(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教程,因为即使你说的不同,你的代码并不表示在你所有的

+0

嗨Tomalek。 非常感谢您的代码。我正在尝试这个,但是当我点击表单提交按钮时,没有任何反应。我之前在xslt中遇到过这个问题。也许这是我在引用xslt.js时做错了什么? – Paul 2010-05-17 19:56:09

+0

这是我参考js的代码: Paul 2010-05-17 19:56:51

+0

与我一起尝试xslt.js和jquery.xslt.js在那里。通常它只是一个。 – Paul 2010-05-17 19:57:17

0

好了,所以我回答我自己的问题: 这里是别人

1)我嵌套我的功能,所以它实际上引用变量的结果。 2)I代替

xml=loadXMLDoc(responseXML2); 

与:

xml=responseXML2; 

现在嵌套函数内工作。